Skip to content

fix: return literal text for unresolved session state variables instead of KeyError#5711

Open
zhoufengen wants to merge 1 commit into
google:mainfrom
zhoufengen:fix/inject-session-state-key-error
Open

fix: return literal text for unresolved session state variables instead of KeyError#5711
zhoufengen wants to merge 1 commit into
google:mainfrom
zhoufengen:fix/inject-session-state-key-error

Conversation

@zhoufengen
Copy link
Copy Markdown

Summary

inject_session_state() raises KeyError when instruction text contains
literal {identifier} patterns — such as documentation describing interpolation
syntax like ${expression}. This crashes any agent that embeds third-party
tool descriptions, JSON schemas, or documentation examples into its instruction
string.

This has already caused a downstream bug in the
A2UI project.

Changes

  • instructions_utils.py: Changed the missing-state branch from raising
    KeyError to returning the literal match text with a debug log. Unresolved
    identifiers now pass through unchanged, matching the existing behavior for
    invalid identifier names.
  • test_instructions_utils.py: Updated the existing KeyError test to
    expect literal text preservation. Added a new regression test for
    {expression} patterns in documentation text.

Testing

pytest tests/unittests/utils/test_instructions_utils.py -v
# 15 passed in 0.60s

Test suite passing on the fix branch:

tests pass

Related Issues

Fixes #5706

…ad of KeyError

inject_session_state raises KeyError when instruction text contains
literal {identifier} patterns (e.g., documentation describing
interpolation syntax like ${expression}). This crashes agents that
embed third-party tool descriptions, JSON schemas, or documentation
examples into instruction strings.

Fix by returning match.group() as-is when a valid identifier is not
found in session state, with a debug log. This matches the existing
behavior for invalid identifier names.
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 15, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

inject_session_state raises KeyError on literal {identifier} patterns in instruction text

1 participant