Repository navigation
Conversation
…as JSON _safe_json_serialize falls back to str() of the whole payload when json.dumps cannot encode a value, so a tool result holding a date, Decimal, enum, UUID, set, bytes or Pydantic model reaches the model as Python repr text instead of JSON. An AgentTool wrapping an agent with an output_schema hits this with no unusual tool code, because the schema is dumped in Python mode. Convert such payloads with pydantic_core.to_jsonable_python, the same options the Gemma path uses, before giving up and using str(). Payloads that json.dumps already handles are serialized exactly as before. Fixes google#8123
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
On the LiteLLM path, a function response becomes the tool message text through
_safe_json_serialize. Whenjson.dumpscan't encode a value in the payload (adate/datetime,Decimal,Enum,UUID,set,bytesor a Pydantic model), the helper falls back tostr()of the whole payload, so the model gets Python repr text instead of JSON.You hit this without any unusual tool code. An
AgentToolthat wraps an agent with anoutput_schemareturns the schema dumped in Python mode, so dates and Decimals end up in the tool result. Repro on main (bee78e7), with a parentLlmAgentonLiteLlm(fake client), calling anAgentToolwhose child agent hasoutput_schema=Invoice(date,Decimal,Enumfields):The Gemini path doesn't have this problem, and the Gemma path already converts these values with
pydantic_core.to_jsonable_python.Solution:
_safe_json_serializestill triesjson.dumpsfirst, so any payload that works today is serialized byte-for-byte the same. If that fails, it converts the payload withto_jsonable_pythonusing the same options as the Gemma path (by_alias=False,exclude_none=True,bytes_mode="base64") and dumps that, keepingensure_ascii=False. Only a payload Pydantic can't serialize either (an arbitrary object, a circular reference) still falls back tostr(), as before.With the change, the same repro prints:
Testing Plan
Unit Tests:
New tests in
tests/unittests/models/test_litellm.py:test_safe_json_serialize_encodes_values_json_module_cannot: date, datetime, Decimal, Enum, UUID, set, bytes and a nested Pydantic model in one payload.test_safe_json_serialize_keeps_non_ascii_text_in_converted_payloadtest_safe_json_serialize_unknown_nested_value_falls_back_to_str: pins the existingstr()fallback.test_content_to_message_param_output_schema_tool_result_is_json: theAgentTool+output_schemashape through_content_to_message_param.Without the source change, 3 of the new tests fail. With it:
pyink, isort, ruff and codespell report no changes or issues on both files.
Manual End-to-End (E2E) Tests:
The repro above: a real
InMemoryRunnerrun where a parentLlmAgentusesLiteLlm(model="openai/gpt-x", llm_client=FakeClient()). The fake client first returns a tool call to anAgentTool, whose childLlmAgent(fake model,output_schema=Invoice) answers with JSON. The script then prints thetoolmessage thatLiteLlmsends on the next call. Python 3.12.Checklist
Additional context
The function-call
argumentspath uses the same helper, so it benefits too. Arguments that come from the model are already plain JSON, so nothing changes for them in practice.