Skip to content

fix(core): preserve output parsers without mutating prompt templates - #23686

Draft
emme1t wants to merge 1 commit into
run-llama:mainfrom
emme1t:fix/nonmutating-prompt-partial-format-20261023
Draft

emme1t wants to merge 1 commit into
run-llama:mainfrom
emme1t:fix/nonmutating-prompt-partial-format-20261023

Conversation

@emme1t

@emme1t emme1t commented Oct 23, 2026

Copy link
Copy Markdown

Description

Fixes #23685.

PromptTemplate.partial_format() clears the source template's parser before copying and restores it only on success. If another value cannot be copied, subsequent calls on the original prompt silently lose their output instructions. ChatPromptTemplate.partial_format() has the opposite inconsistency: it copies the parser itself, so parsers containing locks or non-copyable client state fail to partially format.

Use deepcopy's memo to preserve the parser by identity while copying the other template state. Both template types now retain their parser without temporarily mutating the source. Other copy errors still propagate and leave the original prompt usable. Text-template parser sharing is preserved; chat templates now follow the same behavior.

Validation

  • Unchanged main 619d74664: 2 failed, 2 passed in the new regression module. Chat partial formatting fails on the parser's lock, and failed text partial formatting leaves output_parser=None.
  • After the fix: PYTHONPATH=$PWD python -m pytest tests/prompts/test_base.py tests/prompts/test_utils.py tests/prompts/test_partial_format_parser.py -q --tb=short: 15 passed.
  • Full tests/prompts run: 28 passed, 5 setup errors. All five setup errors are the existing rich-prompt PDF fixture's httpx.get(pdf_url) being rejected by the environment proxy with HTTP 403. They are not counted as passes.
  • Tests cover parser identity, preserved output instructions, original/partial kwargs independence, chat/text prompts, and source preservation after another copy error. No API or model calls are used by the regression tests.
  • Ruff check/format check, Codespell, git diff --check, and targeted mypy with --follow-imports=skip on both changed files: passed. Full repository typechecking/CI was not run locally.

Core-only change; no version bump or new dependency.

AI assistance was used for investigation, implementation and regression tests. Verified both baseline failures and their controls, reviewed parser-sharing behavior, and ran the checks above. Submitted as draft for human review.

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.

[Bug]: partial_format can drop the original output parser or fail to copy chat prompts

1 participant