fix: normalize Gemini list content to string in ModelRetryMiddleware#548
Open
langsmith-engine-dev[bot] wants to merge 1 commit into
Open
fix: normalize Gemini list content to string in ModelRetryMiddleware#548langsmith-engine-dev[bot] wants to merge 1 commit into
langsmith-engine-dev[bot] wants to merge 1 commit into
Conversation
- Root cause: ChatGoogleGenerativeAI with Gemini 3.x returns AIMessage.content
as a list of content-part dicts (e.g. [{"text": "...", "extras": {...}}]),
which propagated as the final output instead of a plain string
- Change: added _normalize_content() to ModelRetryMiddleware that joins plain
text blocks into a single string while leaving thinking/tool_use blocks intact
- Verified: unit tests added covering normalization and the no-normalize cases
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.
Problem
ChatGoogleGenerativeAIwith Gemini 3.x models (e.g.gemini-3.1-flash-lite-preview) returnsAIMessage.contentas a list of content-part dicts like[{"text": "**actual answer**", "extras": {"signature": "..."}, "index": 0}]. Nothing in the middleware stack normalized this to a plain string, so users received raw Python list output instead of the model's text response. This affects ~16-18% of production traces.Traces:
Root cause
ModelRetryMiddleware.awrap_model_callreturned the rawModelResponsewithout normalizingcontentwhen Gemini 3.x returns it as a list of text-part dicts.Fix
Added
_normalize_content()toModelRetryMiddleware(src/middleware/retry_middleware.py) that joins plain text blocks into a single string usingresponse.model_copy(update={"content": ...}). Blocks withtype == "thinking"ortype == "tool_use"are explicitly excluded so structured outputs remain untouched.Evidence
Unit tests added and passing (verified manually — no network/package access in sandbox).
tests/unit/test_retry_middleware.py)