fix(core): skip empty text blocks in ChatOpenAI contentBlocks - #9995
Conversation
When ChatOpenAI returns tool calls without text content, the content
field is often an empty string. Previously this would result in an
empty text block { type: 'text', text: '' } being included in the
contentBlocks array alongside the tool calls.
This fix skips adding text blocks when content is an empty string
in both convertToV1FromChatCompletions and convertToV1FromChatCompletionsChunk.
Fixes langchain-ai#9955
🦋 Changeset detectedLatest commit: 1de94cf The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Kai Gritun (@kaigritun) thanks for the contribution. Would you mind sharing your motivation for this change? Did you run into any errors or is there another reason why we wouldn't want to have this empty text content block? |
|
Thanks for the question! This is linked to issue #9955 - when ChatOpenAI returns tool calls, the While it doesn't cause errors directly, these empty blocks add noise to the content array and can cause issues for downstream consumers that expect meaningful content in text blocks. The fix simply skips adding a text block when |
8f166b1
into
langchain-ai:main
|
I'm not making any claims about the quality of their work, but I wanted to let you know that Kai Gritun (@kaigritun) is a fully-autonomous non-human actor |
…ain-ai#9995) Co-authored-by: Kai Gritun <kai@kaigritun.com> Co-authored-by: Christian Bromann <git@bromann.dev>
|
Thanks for flagging Michaël De Boey (@MichaelDeBoey) |
…ain-ai#9995) Co-authored-by: Kai Gritun <kai@kaigritun.com> Co-authored-by: Christian Bromann <git@bromann.dev>
Problem
When ChatOpenAI returns tool calls without text content, the content field is often an empty string (
""). Previously this would result in an empty text block being included in thecontentBlocksarray:[ { "type": "text", "text": "" }, { "type": "tool_call", "id": "call_123", "name": "get_value", "args": { "key": "a" } } ]Solution
Skip adding text blocks when content is an empty string in both
convertToV1FromChatCompletionsandconvertToV1FromChatCompletionsChunk.Now the output correctly excludes empty text blocks:
[ { "type": "tool_call", "id": "call_123", "name": "get_value", "args": { "key": "a" } } ]Changes
convertToV1FromChatCompletionsto checkmessage.content.length > 0before adding text blockconvertToV1FromChatCompletionsChunkwith the same fixTesting
All 8 tests in
openai.test.tspass (6 existing + 2 new).Fixes #9955