Skip to content

fix(core): skip empty text blocks in ChatOpenAI contentBlocks - #9995

Merged
Christian Bromann (christian-bromann) merged 2 commits into
langchain-ai:mainfrom
kaigritun:fix/empty-text-blocks-tool-calls
Feb 11, 2026
Merged

Christian Bromann (christian-bromann) merged 2 commits into
langchain-ai:mainfrom
kaigritun:fix/empty-text-blocks-tool-calls

Conversation

@kaigritun

Copy link
Copy Markdown
Contributor

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 the contentBlocks array:

[
  {
    "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 convertToV1FromChatCompletions and convertToV1FromChatCompletionsChunk.

Now the output correctly excludes empty text blocks:

[
  {
    "type": "tool_call",
    "id": "call_123",
    "name": "get_value",
    "args": { "key": "a" }
  }
]

Changes

  • Modified convertToV1FromChatCompletions to check message.content.length > 0 before adding text block
  • Modified convertToV1FromChatCompletionsChunk with the same fix
  • Added 2 regression tests to verify the fix

Testing

All 8 tests in openai.test.ts pass (6 existing + 2 new).

Fixes #9955

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-bot

changeset-bot Bot commented Feb 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1de94cf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@langchain/core Patch
@langchain/community Patch
langchain Patch
@langchain/anthropic Patch
@langchain/google-cloud-sql-pg Patch
@langchain/google-common Patch
@langchain/google-genai Patch
@langchain/google-webauth Patch
@langchain/model-profiles Patch
@langchain/standard-tests Patch
@langchain/google-gauth Patch
@langchain/google-vertexai-web Patch
@langchain/google-vertexai Patch

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

@christian-bromann

Copy link
Copy Markdown
Member

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?

@kaigritun

Copy link
Copy Markdown
Contributor Author

Thanks for the question! This is linked to issue #9955 - when ChatOpenAI returns tool calls, the content field is often an empty string "". This creates an empty text block { type: "text", text: "" } alongside the tool call blocks.

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 message.content is empty, making the output cleaner and consistent with what the model actually returned (just tool calls, no text).

@christian-bromann
Christian Bromann (christian-bromann) merged commit 8f166b1 into langchain-ai:main Feb 11, 2026
38 checks passed
@MichaelDeBoey

Copy link
Copy Markdown

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
https://socket.dev/blog/ai-agent-lands-prs-in-major-oss-projects-targets-maintainers-via-cold-outreach

Mia (miadisabelle) pushed a commit to avadisabelle/ava-langchainjs that referenced this pull request Feb 18, 2026
…ain-ai#9995)

Co-authored-by: Kai Gritun <kai@kaigritun.com>
Co-authored-by: Christian Bromann <git@bromann.dev>
@christian-bromann

Copy link
Copy Markdown
Member

Thanks for flagging Michaël De Boey (@MichaelDeBoey)

Nick Winder (nickwinder) pushed a commit to nickwinder/langchainjs that referenced this pull request Mar 24, 2026
…ain-ai#9995)

Co-authored-by: Kai Gritun <kai@kaigritun.com>
Co-authored-by: Christian Bromann <git@bromann.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatOpenAI returns empty content blocks when doing tool calls

3 participants