fix: normalize tool results to strings for OpenAI API compatibility#409
fix: normalize tool results to strings for OpenAI API compatibility#409Ayaan Ahmad (czarflix) wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
Addresses langchain-ai/langchain#34669 OpenAI's Chat Completion API strictly requires tool_message content to be strings. The previous list[dict] format caused 400 BadRequestError crashes. Changes: - Single text blocks -> plain string - Multiple text blocks -> newline-joined string - Mixed/Complex content -> JSON-serialized string - Updated tests to assert string output This restores functionality for OpenAI while maintaining compatibility with Anthropic (via string parsing).
|
Furkan Zeki ÖZYURT (@fzozyurt) thanks for the review. Could a maintainer approve the workflows? Tagging Bagatur (@baskaryan) for visibility. |
|
What if MCP returns an image which is expected to be understood by LLM for feature extraction? It cannot read bytes/base64 data. I guess openai responses can interpret list[dict] supporting multimodal capabilities. |
Yeah so Chat Completions API doesn't actually support images in tool messages - it throws a 400 if you try. This fix just makes sure we don't crash. For image understanding you'd need the Responses API which langchain-openai doesn't use by default yet. Could be a follow-up. |
🐛 Bug Fix
Issue
Addresses langchain-ai/langchain#34669
Users reported receiving
400 BadRequestErrorfrom OpenAI/Azure Chat Models when using MCP tools. The errorInput should be a valid dictionary or instance of Contentoccurs because the adapter was returning tool content as alist[dict](e.g.,[{"type": "text", "text": "..."}]).Root Cause: OpenAI's Chat Completion API strictly requires
tool_messagecontent to be a string (not an array of content blocks).The Fix
This PR normalizes MCP tool results into a string format compatible with
langchain-openai:str(e.g.,"Result")str(e.g.,"Line 1\nLine 2")str(e.g.,'[{"type":"image",...}]')Compatibility Matrix
Verification
list[dict]format.tests/test_tools.py. Note: Previous tests assertedlistoutput (the buggy behavior). I rewrote these assertions to expect the new normalizedstroutput.Change Impact
This PR changes the return type of
_convert_call_tool_resultfromlist[dict]tostr.Why this is NOT a breaking change:
list[dict]format caused crashes with OpenAI (the primary consumer).MCPToolArtifactis still preserved and returned if downstream users need the original objects.Proposed Release: Patch (Bug Fix)
Maintainer Note: This issue is tracked in the main
langchainrepo (#34669). Since cross-repo auto-close is unreliable, I can manually update that issue once this is merged.