Repository navigation
fix(models): stop Gemma 3 failing on datetime or Pydantic tool results - #7668
Open
roydonsequeira wants to merge 2 commits into
Open
roydonsequeira wants to merge 2 commits into
roydonsequeira wants to merge 2 commits into
Conversation
Gemma and Gemma3Ollama rendered each function response as text with a bare json.dumps, but a tool result holds whatever the tool returned. A datetime, bytes or a Pydantic model, such as the LoadMemoryResponse of the built-in load_memory tool, raised TypeError on the next model call and failed the run, and non-ASCII text reached the model as \uXXXX escapes. Serialize the payload with to_jsonable_python the way a Gemini request carries a function response, and keep non-ASCII text as is. Fixes google#7667
|
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. |
A None in the tool's own dicts and lists stays null in the text Gemma 3 reads, as it does in a Gemini request; exclude_none only drops Pydantic model fields that are None. Pin that with a test case, and correct the docstring, which described the dropped fields as unset.
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.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
load_memory) #7667Problem:
GemmaandGemma3Ollamarender each function response as text for Gemma 3 with a barejson.dumps(func_response.response). A tool result is stored as the tool returned it, so adatetime,bytesor a Pydantic model raisedTypeErroron the next model call and failed the run. The built-inload_memorytool always hit this, because it returns aLoadMemoryResponse. The defaultensure_ascii=Truealso sent non-ASCII text in tool results to the model as\uXXXXescapes.Solution:
Serialize the payload with
pydantic_core.to_jsonable_python, the way a Gemini request carries the same function response: unset model fields are dropped (as google-genai does withexclude_none=True), bytes are base64-encoded, and values Pydantic cannot serialize fall back to theirstr().json.dumps(..., ensure_ascii=False)then keeps non-ASCII text as is. Payloads that already were plain JSON render exactly as before, so the existing tests are unchanged.The change is in
GemmaFunctionCallingMixin's content conversion, which bothGemmaandGemma3Ollamause. Gemma 4 uses native function calling and is not affected.Testing Plan
Unit Tests:
test_preprocess_request_with_function_response_converts_valuesintests/unittests/models/test_gemma_llm.pycovers non-ASCII text, adatetime,bytesand aLoadMemoryResponse. All four cases fail onmain(three withTypeError, one with escaped text) and pass with the fix.Full suite on Windows 11, Python 3.11 (
pytest tests/unittests -n 4): 73 failed, 20749 passed, 107 skipped. Every one of the 73 also fails on unmodifiedmainin this environment, all outsidemodels/(Windows-specific failures such astest_cli_deploy_to_cloud_run.pyandtest_samples.py).test_gemma_llm.pyalso passes on Python 3.12, 3.13 and 3.14.Manual End-to-End (E2E) Tests:
An
AgentonGemma3Ollama(model="ollama/gemma3:4b")(Ollama 0.40.7), run throughRunnerwithInMemorySessionService. Each run prints the tool-result text that reaches the model.Function tool whose result holds a
datetimeand non-ASCII text:Built-in
load_memorywith one remembered session inInMemoryMemoryService:Checklist
Additional context
The formatters, linters and repository checks from
.pre-commit-config.yamlpass on both files, and mypy reports no errors ingemma_llm.py, as onmain.