Skip to content

Conversation

binaryCrossEntropy
Copy link
Contributor

@binaryCrossEntropy binaryCrossEntropy commented Aug 28, 2025

Gemini models (google-genai >= 1.31.0) can emit streaming parts that only
contain executable_code or code_execution_result with no text. Our
streaming handler asserted on these parts, causing an AssertionError.

This change:

  • Ignores chunks that only have executable_code or code_execution_result
  • Mirrors existing behavior for Anthropic/OpenAI where similar built-in
    tool calls are skipped during streaming
  • Prevents AssertionError when combining Gemini streaming with
    CodeExecutionTool

Closes #2626 #2626

@DouweM DouweM changed the title fix(gemini-streaming): AssertionError (Issue#2626) Fix error when streaming from Gemini includes only executable_code or code_execution_result Aug 29, 2025
@DouweM
Copy link
Collaborator

DouweM commented Aug 29, 2025

@binaryCrossEntropy Thank you! Can you see if you can add a test that triggers this code path? Likely just using the code execution built-in tool will do it.

@binaryCrossEntropy
Copy link
Contributor Author

binaryCrossEntropy commented Aug 31, 2025

Hiya @DouweM - I’ve added a test that triggers the path where Gemini streams executable_code / code_execution_result.
I put my test case in tests/models/test_google.py and also added a cassette file.

Files modified/added:

  • Test: tests/models/test_google.py::test_google_model_builtin_code_execution_stream

  • Cassette: tests/fixtures/cassettes/test_google_model_builtin_code_execution_stream.yaml

The test passes with this fix, and it fails with an AssertionError if I remove the code.

Image (ignore the function name I modified it later)

Test pass:
test_pass

Happy to work on other feedback :)

@DouweM DouweM enabled auto-merge (squash) September 1, 2025 20:00
@DouweM
Copy link
Collaborator

DouweM commented Sep 1, 2025

@binaryCrossEntropy Thanks Iqbal!

@DouweM DouweM merged commit f4a6f0e into pydantic:main Sep 1, 2025
58 of 60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assertion error when using Gemini Models in streaming mode ALONG with CodeExecutionTool()
2 participants