Repository navigation
fix(examples): render function responses in examples as JSON - #7964
Open
1828703450qqcom-ux wants to merge 1 commit into
Open
1828703450qqcom-ux wants to merge 1 commit into
1828703450qqcom-ux wants to merge 1 commit into
Conversation
convert_examples_to_text is documented to convert a function response part to a json string, but it rendered part.function_response.__dict__, the pydantic model's internal storage. That leaks every unset optional field as None into the few-shot example and renders nested values through their Python repr, which spans several lines and breaks the fenced block around the example. Serialize the response fields as JSON with the unset optional fields dropped, so the example shows the response shape the model is being taught. Non-JSON-serializable values fall back to their string form rather than failing the system-instruction build.
1828703450qqcom-ux
force-pushed
the
fix/examples-function-response-json
branch
from
November 4, 2026 10:24
c1b315e to
e1c5d82
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(examples): render function responses in examples as JSON
Summary
convert_examples_to_textis documented to "Convert function response partto json string", but it rendered
part.function_response.__dict__— thepydantic model's internal storage.
That leaks every unset optional field as
Noneinto the few-shot example,so the model is taught a tool response looks like:
It is also a Python repr, not JSON:
None/True/single quotes, and anynested pydantic value inside
responserenders through its repr, whichspans several lines and breaks the fenced block that wraps the example.
Fix: serialize the response fields as JSON with the unset optional fields
dropped, so the example shows the response shape only.
Reproduction
Public API, on the current tree:
Before, inside the fence:
After:
The nested-value case is worse before — a
types.Partin the response dictrenders as
Part(\n text='nested_value'\n), so the fenced block contains rawnewlines and the example no longer reads as one tool-output payload. After the
change the same input renders
{"nested": {"text": "nested_value"}}on oneline.
Fix
src/google/adk/examples/example_util.py— the function-response branch nowcalls a small
_function_response_jsonhelper that emitsjson.dumps(response.model_dump(exclude_none=True), default=str).model_dumpalso recurses into nested pydantic values, so they become JSONobjects instead of multi-line reprs. A non-JSON-serializable value falls back
to its string form rather than failing the whole system-instruction build.
The function-call branch is unchanged: it is documented to render a
python-like call and its existing tests pin that shape.
Validation
Two new regression tests in
tests/unittests/examples/test_example_util.py:will_continue/scheduling/partsleak, Python reprPart(\n text=...)breaks the fenceBoth fail on the unmodified tree (verified by stashing only the source file)
and pass after. The exact before-output is visible in the assertion diff of
test_function_response_nested_values_render_as_json.The three existing tests that pinned
f"{test_function_response.__dict__}"in their expected string now expect the JSON form; no other expectations
changed. Those tests had echoed the implementation's
__dict__rather thanspecifying an output format.
tests/unittests/examples/: 76 passed.tests/unittests/examples/+tests/unittests/tools/: 3580 passed,2 skipped, 4 failed. The 4 failures are
tests/unittests/tools/test_skill_toolset.pyPOSIX-environment cases(shell metacharacters, FIFO, symlink) and reproduce identically on
unmodified
main; this change does not touch that module.Success: no issues found.ruff,isort,pyink,addlicense,codespell,fix end of files,trim trailing whitespaceall pass. The threenative-Windows shebang hooks (
check-new-py-prefix,compliance-checks,check-docs) cannot start/bin/bashon this host and were run directlythrough Python instead:
scripts/check_new_py_files.py(git-detectionmode),
scripts/compliance_checks.pyandscripts/check_docs.pyall exit0. This change adds no new Python files, so the prefix/unit-guide rule is
not engaged.
Scope
src/google/adk/examples/example_util.pyandtests/unittests/examples/test_example_util.py.work, or to the route-preservation change already imported into main.