Skip to content

[REFACTOR]: Decouple PromptTemplate creation from YAML - #156

Merged
Spencer Schoenberg (spencrr) merged 1 commit into
microsoft:mainfrom
spencrr:dev/spencrr/refactor-prompt-template-creation
Aug 7, 2026
Merged

Spencer Schoenberg (spencrr) merged 1 commit into
microsoft:mainfrom
spencrr:dev/spencrr/refactor-prompt-template-creation

Conversation

@spencrr

Copy link
Copy Markdown
Contributor

Description

This supersedes the constructor approaches in #132 and #144 and follows the review feedback on #144.

This PR makes prompt-template construction source-neutral while retaining PromptTemplate.from_yaml(path) as the YAML and filesystem adapter.

  • Adds a public, strict, frozen PromptTemplateDefinition.
  • Normalizes declared parameters to an immutable, ordered tuple.
  • Prevents prompt source from appearing in definition or template representations.
  • Replaces injectable dataclass construction with PromptTemplate(definition=...).
  • Compiles and validates Jinja exclusively inside PromptTemplate.
  • Preserves from_yaml(path) and both existing production call sites.
  • Keeps YAML paths outside the definition and compiled runtime object.
  • Preserves direct underlying exception causes while adding YAML path context.
  • Reports Jinja line numbers explicitly as template-value lines rather than misleading physical YAML lines.
  • Replaces metadata-based equality with object identity.

Jinja receives neither the YAML path nor the logical template name. This avoids incorrect YAML file/line attribution and prevents names such as summary.html from unexpectedly enabling autoescape.

Breaking changes

Direct component-wise construction is no longer accepted:

PromptTemplate(
    name="Greeting",
    description=None,
    parameter_keys=("subject",),
    _template=compiled_template,
)

Construct from a validated definition instead:

PromptTemplate(
    definition=PromptTemplateDefinition(
        name="Greeting",
        description=None,
        parameters=("subject",),
        value="Hello, {{ subject }}!",
    ),
)

PromptTemplate.from_yaml(path) is unchanged, so existing supported call sites require no migration.

Separate PromptTemplate instances now use identity equality and hashing rather than metadata-based structural equality. PromptTemplateDefinitionError.path is optional for in-memory construction.

Checklist

  • pre-commit run --all-files passes
  • Tests added or updated for changes
    • 34 focused prompt-template tests pass
    • 652 tracked unit tests pass
  • Documentation updated
    • Module and public API docstrings describe the source-neutral construction and error contracts

@spencrr
Spencer Schoenberg (spencrr) requested a review from a team August 6, 2026 23:20
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread rampart/common/templates.py
Comment thread rampart/common/templates.py

@nina-msft Nina Chikanov (nina-msft) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great implementation based off of my feedback from previous PR! I did not get a chance to run this through GHCP because it is being very slow for me today - so if you haven't done a self-review through that it may be worth to make sure we didn't miss anything.

Otherwise, small feedback re: exception handling and otherwise I think the direction looks good. Up to you if you'd also like Bashir Partovi (@bashirpartovi) to take a look before merge.

@spencrr
Spencer Schoenberg (spencrr) merged commit de62d77 into microsoft:main Aug 7, 2026
14 checks passed
@spencrr
Spencer Schoenberg (spencrr) deleted the dev/spencrr/refactor-prompt-template-creation branch August 8, 2026 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants