fix(langchain): normalise provider-specific content blocks in ModelFallbackMiddleware#36601
Open
VishwasPatel (Vishwaspatel2401) wants to merge 1 commit intolangchain-ai:masterfrom
Conversation
…llbackMiddleware When ModelFallbackMiddleware falls back to an OpenAI-based model, the response can contain function_call content blocks in OpenAI's internal format. If that response is added to message history and forwarded to a primary model that only understands LangChain-standard tool_use blocks (e.g. ChatBedrockConverse), the primary model raises an error on the unrecognised block type. This fix adds two normalisation helpers: - _normalise_ai_message: converts function_call blocks to standard tool_use, handling string/dict arguments and id/callId/call_id aliases - _normalise_fallback_response: applies normalisation across all result messages after a fallback model succeeds The primary model path is deliberately left unchanged. Co-authored-by: Snir Balgaly <balgaly@users.noreply.github.com> closes: langchain-ai#36531
Snir Balgaly (balgaly)
left a comment
There was a problem hiding this comment.
Good coverage overall. One test case worth adding: a check that the primary model path returns unchanged (no normalisation). The current tests verify the fallback path converts correctly, but there is no test asserting that a primary handler result with function_call blocks passes through as-is.
Something like:
def test_primary_path_not_normalised() -> None:
"""Primary handler result is returned unchanged -- normalisation only applies in fallback path."""
from langchain.agents.middleware.model_fallback import ModelFallbackMiddleware
primary_msg = AIMessage(
content=[{"type": "function_call", "id": "c1", "name": "fn", "arguments": "{}"}]
)
primary_response = ModelResponse(result=[primary_msg])
middleware = ModelFallbackMiddleware.__new__(ModelFallbackMiddleware)
middleware.models = []
result = middleware.wrap_model_call(object(), lambda _req: primary_response) # type: ignore[arg-type]
assert result.result[0].content[0]["type"] == "function_call"This locks in the invariant that normalisation is scoped only to fallback, which is the key design decision here.
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.
Fixes #36531
When ModelFallbackMiddleware falls back to an OpenAI-based model, the response
can contain
function_callcontent blocks in OpenAI's internal format. If thatresponse is added to message history and forwarded to a primary model that only
understands LangChain-standard
tool_useblocks (e.g.ChatBedrockConverse),the primary model raises an error on the unrecognised block type.
This fix adds two normalisation helpers to
ModelFallbackMiddleware:_normalise_ai_message: convertsfunction_callblocks to standardtool_use,handling string/dict arguments and
id/callId/call_idaliases_normalise_fallback_response: applies normalisation across all result messagesafter a fallback model succeeds
The primary model path is deliberately left unchanged.
Co-authored with Snir Balgaly (@balgaly) who drafted the initial implementation in #36600.
Verification
Unit tests added in
tests/unit_tests/agents/middleware/test_model_fallback_normalise.pycovering string/dict arguments, id alias resolution, mixed content blocks,
invalid JSON handling, and verification that the primary model path is NOT normalised.