Skip to content

fix(models): send LiteLlm tool results with dates, Decimals or enums as JSON - #8449

Open
tanvir-ux wants to merge 1 commit into
google:mainfrom
tanvir-ux:fix-litellm-tool-result-json
Open

tanvir-ux wants to merge 1 commit into
google:mainfrom
tanvir-ux:fix-litellm-tool-result-json

Conversation

@tanvir-ux

Copy link
Copy Markdown

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. When json.dumps can't encode a value in the payload (a date/datetime, Decimal, Enum, UUID, set, bytes or a Pydantic model), the helper falls back to str() of the whole payload, so the model gets Python repr text instead of JSON.

You hit this without any unusual tool code. An AgentTool that wraps an agent with an output_schema returns the schema dumped in Python mode, so dates and Decimals end up in the tool result. Repro on main (bee78e7), with a parent LlmAgent on LiteLlm (fake client), calling an AgentTool whose child agent has output_schema=Invoice (date, Decimal, Enum fields):

tool message sent to LiteLLM: {'due_date': datetime.date(2026, 12, 1), 'total': Decimal('12.50'), 'status': 'paid'}
valid JSON: no

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_serialize still tries json.dumps first, so any payload that works today is serialized byte-for-byte the same. If that fails, it converts the payload with to_jsonable_python using the same options as the Gemma path (by_alias=False, exclude_none=True, bytes_mode="base64") and dumps that, keeping ensure_ascii=False. Only a payload Pydantic can't serialize either (an arbitrary object, a circular reference) still falls back to str(), as before.

With the change, the same repro prints:

tool message sent to LiteLLM: {"due_date": "2026-12-01", "total": "12.50", "status": "paid"}
valid JSON: yes

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

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_payload
  • test_safe_json_serialize_unknown_nested_value_falls_back_to_str: pins the existing str() fallback.
  • test_content_to_message_param_output_schema_tool_result_is_json: the AgentTool + output_schema shape through _content_to_message_param.

Without the source change, 3 of the new tests fail. With it:

$ pytest tests/unittests/models/test_litellm.py -k "safe_json or output_schema_tool_result"
9 passed, 885 deselected

$ pytest tests/unittests/models
2238 passed

$ pytest tests/unittests/models tests/unittests/tools/test_agent_tool.py tests/unittests/flows
3563 passed

pyink, isort, ruff and codespell report no changes or issues on both files.

Manual End-to-End (E2E) Tests:

The repro above: a real InMemoryRunner run where a parent LlmAgent uses LiteLlm(model="openai/gpt-x", llm_client=FakeClient()). The fake client first returns a tool call to an AgentTool, whose child LlmAgent (fake model, output_schema=Invoice) answers with JSON. The script then prints the tool message that LiteLlm sends on the next call. Python 3.12.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The function-call arguments path 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.

…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
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.

LiteLlm sends tool results that hold a date, Decimal or Pydantic model to the model as a Python repr

1 participant