Python POML Core APIs¶
Estimated time to read: 1 minute
poml
¶
clear_trace()
¶
get_trace()
¶
poml(markup, context=None, stylesheet=None, chat=True, output_file=None, format='message_dict', extra_args=None)
¶
Process POML markup and return the result in the specified format.
POML (Prompt Orchestration Markup Language) is a markup language for creating structured prompts and conversations. This function processes POML markup with optional context and styling, returning the result in various formats optimized for different LLM frameworks and use cases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
markup
|
str | Path
|
POML markup content as a string, or path to a POML file. If a string that looks like a file path but doesn't exist, a warning is issued and it's treated as markup content. |
required |
context
|
dict | str | Path | None
|
Optional context data to inject into the POML template. Can be a dictionary, JSON string, or path to a JSON file. |
None
|
stylesheet
|
dict | str | Path | None
|
Optional stylesheet for customizing POML rendering. Can be a dictionary, JSON string, or path to a JSON file. |
None
|
chat
|
bool
|
If True, process as a chat conversation (default). If False, process as a single prompt. |
True
|
output_file
|
str | Path | None
|
Optional path to save the output. If not provided, output is returned directly without saving to disk. |
None
|
format
|
OutputFormat
|
Output format for the result: - "raw": Raw string output from POML processor - "message_dict": Legacy format returning just messages array (default) - "dict": Full CLI result structure with messages, schema, tools, runtime - "openai_chat": OpenAI Chat Completion API format with tool support - "langchain": LangChain message format with structured data - "pydantic": PomlFrame object with typed Pydantic models |
'message_dict'
|
extra_args
|
Optional[List[str]]
|
Additional command-line arguments to pass to the POML processor. |
None
|
Returns:
Name | Type | Description |
---|---|---|
list | dict | str | PomlFrame
|
The processed result in the specified format: |
|
list | dict | str | PomlFrame
|
|
|
list | dict | str | PomlFrame
|
|
|
list | dict | str | PomlFrame
|
|
|
list | dict | str | PomlFrame
|
|
|
list | dict | str | PomlFrame
|
For format="message_dict": Returns just the messages array for backward |
|
list | dict | str | PomlFrame
|
compatibility. Example: |
|
list | dict | str | PomlFrame
|
For format="dict": Returns complete structure with all metadata. |
|
Example |
list | dict | str | PomlFrame
|
|
list | dict | str | PomlFrame
|
For format="openai_chat": Returns OpenAI Chat Completion format with tool/schema |
|
list | dict | str | PomlFrame
|
support. Includes "messages" in OpenAI format, "tools" if present, "response_format" |
|
list | dict | str | PomlFrame
|
for JSON schema if present, and runtime parameters converted to |
|
list | dict | str | PomlFrame
|
For format="langchain": Returns LangChain format preserving all metadata with |
|
list | dict | str | PomlFrame
|
"messages" in LangChain format plus schema, tools, and runtime if present. |
|
list | dict | str | PomlFrame
|
For format="pydantic": Returns strongly-typed PomlFrame object containing |
|
list | dict | str | PomlFrame
|
messages as PomlMessage objects, output_schema, tools, and runtime. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
When a specified file path doesn't exist. |
RuntimeError
|
When the POML processor fails or backend tracing requirements aren't met. |
ValueError
|
When an invalid output format is specified. |
Examples:
Basic usage with markup string:
Load from file with context:
Get OpenAI chat format:
Use with custom stylesheet:
>>> result = poml(
... markup="template.poml",
... context={"user": "Alice"},
... stylesheet={"role": {"captionStyle": "bold"}},
... format="pydantic"
... )
Save output to file:
Note
- When tracing is enabled via set_trace(), call details are automatically logged
- The function supports various backend integrations (Weave, AgentOps, MLflow)
- Multi-modal content (images, etc.) is supported in chat format
Source code in python/poml/api.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
|
set_trace(enabled=True, /, *, trace_dir=None)
¶
Enable or disable tracing of poml
calls with optional backend integrations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
enabled
|
bool | List[Backend] | Backend
|
Controls which tracing backends to enable. Can be: - True: Enable local tracing only (equivalent to ["local"]) - False: Disable all tracing (equivalent to []) - str: Enable a single backend ("local", "weave", "agentops", "mlflow") - List[str]: Enable multiple backends. "local" is auto-enabled if any backends are specified. |
True
|
trace_dir
|
Optional[str | Path]
|
Optional directory for local trace files. If provided when local tracing is enabled, a subdirectory named by the current timestamp (YYYYMMDDHHMMSSffffff) is created inside trace_dir. |
None
|
Returns:
Type | Description |
---|---|
Optional[Path]
|
Path to the trace directory if local tracing is enabled, None otherwise. |
Optional[Path]
|
The directory may be shared with POML Node.js by setting the |
Optional[Path]
|
POML_TRACE environment variable in the invoking script. |
Available backends
- "local": Save trace files to disk
- "weave": Log to Weights & Biases Weave (requires local tracing)
- "agentops": Log to AgentOps (requires local tracing)
- "mlflow": Log to MLflow (requires local tracing)
Source code in python/poml/api.py
trace_artifact(file_suffix, contents)
¶
Write an additional artifact file for the most recent poml
call. This API is experimental.