Repository navigation
Conversation
OpenAILlm sent a tool result with a bare json.dumps. A tool that returns a datetime, bytes or a Pydantic model made the next request raise TypeError, so the run failed on the first tool turn. ADK's own load_memory tool returns such a model, so every OpenAILlm agent that uses it failed. The same result sent to Gemini works. json.dumps now gets a default that converts these values with pydantic_core.to_jsonable_python, as the Gemma fix does: ISO datetimes, base64 bytes, models as dicts with None fields dropped and field names not aliased, and str() as a last resort. The default only runs on values that raised before, so results that already worked are sent unchanged.
lxcxjxhx
left a comment
There was a problem hiding this comment.
The default= hook is the right mechanism: it fires only for the values json.dumps cannot encode, so plain-dict tool results serialize exactly as before and nothing changes for the common case.
One behaviour change worth naming, since it is not in the docstring: to_jsonable_python(..., exclude_none=True) drops None fields from Pydantic models, so a tool result that used to serialize as {"status": null, "value": 1} now becomes {"value": 1}. That matches what the Gemini path does — the docstring says "as in a Gemini request", which is the right reference — but on the OpenAI path it is new: before this change a None inside a model came out as null, because json.dumps had no trouble with it and never called default. If any downstream consumer reads status and distinguishes "absent" from "null", this changes what they see.
If that is intended, a clause in the docstring ("None fields of a model are dropped, matching the Gemini request shape") records it. If it is not, exclude_none=False keeps the old shape and the datetime/bytes handling still works.
Checked and correct: default is called per unencodable leaf, not once for the whole structure, so a list of datetimes and a dict with one datetime inside both work — to_jsonable_python receives the leaf in each case.
The three cases in the title (datetime, bytes, models) map to the three to_jsonable_python arguments (serialize_unknown/bytes_mode/Pydantic handling), which makes the test names worth reading to confirm each one is actually pinned.
|
@lxcxjxhx Thanks for the review. default= only runs for values json.dumps can't encode, so a Pydantic model here used to raise TypeError; tool results that serialized before are sent exactly as before. The non-None behavior is in the helper's docstring and matches the Gemma fix (#7668). Happy to switch to exclude_none=False if prefered. |
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):
load_memory) #7667 / fix(models): stop Gemma 3 failing on datetime or Pydantic tool results #7668 (the same crash, fixed for Gemma 3)2. Or, if no issue exists, describe the change:
Problem:
OpenAILlmbuilds each tool message with a barejson.dumps(part.function_response.response)(_openai_llm.py:147). Tool results reach the adapter as Python objects:_caller.pywraps a non-dict result as{'result': value}and keeps it as is. So when a tool returns adatetime,bytesor a Pydantic model, the next request raisesTypeErrorand the run stops.ADK's own
load_memorytool returns a Pydantic model (LoadMemoryResponse), so everyOpenAILlmagent that usesload_memoryfails on the turn after the tool runs. The same agent works on Gemini.Repro (a real
InMemoryRunner,load_memory_toolandOpenAILlm; onlychat.completions.createis stubbed):On main:
A tool that returns
{"created": datetime(...)}fails the same way:TypeError: Object of type datetime is not JSON serializable.Solution:
Pass a
default=hook to thatjson.dumps. The hook callspydantic_core.to_jsonable_pythonwith the same settings as the Gemma fix (#7668):by_alias=False,exclude_none=True,bytes_mode="base64",serialize_unknown=True. So datetimes become ISO 8601 text, bytes become base64 text, models become dicts of their non-Nonefields, and values Pydantic cannot convert fall back to theirstr().json.dumpsonly callsdefaultfor values it cannot encode itself, so every tool result that works today is sent byte for byte the same (sameensure_ascii, same layout). Only results that used to raise change. The siblingOpenAIResponsesLlmin the same package already passesdefault=str(_openai_responses_llm.py:169-170).With this change the repro prints:
Testing Plan
Unit Tests:
New parametrized test
test_content_to_openai_messages_encodes_tool_result_valuesintests/unittests/integrations/openai/test_openai_llm.py, with one case each for adatetime,bytes, a Pydantic model, aLoadMemoryResponse, and a plain JSON result (non-ASCII text and a nestedNone) whose output must stay exactly whatjson.dumpsgives today.Without the source change, the four non-JSON cases fail with their own
TypeErrorand the plain case passes (4 failed, 66 passed). With it:pyink and isort leave both files unchanged.
Manual End-to-End (E2E) Tests:
I don't have an OpenAI key, so the repro above stands in for a live run: a real
InMemoryRunner,InMemoryMemoryService,load_memory_toolandOpenAILlm, withchat.completions.createstubbed to return realChatCompletionobjects. Its output on main and with this change is shown above.Checklist
Additional context
Values
json.dumpsrejects without callingdefault(dict keys that are notstr,int,float,boolorNone, and circular references) still raise as before; that is out of scope here.