Skip to content

fix(anthropic): handle empty message content gracefully - #35504

Closed
Jason Meng (JasonCZMeng) wants to merge 3 commits into
langchain-ai:masterfrom
JasonCZMeng:fix/issue-35081
Closed

Jason Meng (JasonCZMeng) wants to merge 3 commits into
langchain-ai:masterfrom
JasonCZMeng:fix/issue-35081

Conversation

@JasonCZMeng

Copy link
Copy Markdown
Contributor

Summary

Replace empty string content in human and system messages with a single space ( ) before sending to the Anthropic API. This prevents a BadRequestError (400) when HumanMessage or SystemMessage has content="".

Fixes #35081

Motivation

Anthropic's API rejects messages with empty content: "all messages must have non-empty content except for the optional final assistant message". This is a problem in multi-agent pipelines where one agent's empty output becomes the next agent's user input. The same approach was adopted by AutoGen.

Changes

libs/partners/anthropic/langchain_anthropic/chat_models.py

  • In _format_messages(), empty string content ("") in human and system messages is replaced with " " before calling the API
  • Handles string content, list content blocks, and dict text blocks
  • Also fixes a minor inefficiency in tool_result content formatting for plain string content

libs/partners/anthropic/tests/unit_tests/test_chat_models.py

  • Added test__format_messages_with_empty_system_and_human_content() covering the fix

Replace empty string content in human and system messages with a single
space before sending to the Anthropic API. This prevents a 400
BadRequestError when HumanMessage or SystemMessage has content=''.

Fixes langchain-ai#35081
@github-actions github-actions Bot added external integration PR made that is related to a provider partner package integration anthropic `langchain-anthropic` package issues & PRs and removed external labels Mar 2, 2026
@codspeed

codspeed Bot commented Mar 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks
⏩ 33 skipped benchmarks1


Comparing JasonCZMeng:fix/issue-35081 (6215543) with master (29134dc)

Open in CodSpeed

Footnotes

  1. 33 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@JasonCZMeng Jason Meng (JasonCZMeng) changed the title anthropic: handle empty message content gracefully fix(anthropic): handle empty message content gracefully Mar 2, 2026
@github-actions github-actions Bot added the fix For PRs that implement a fix label Mar 2, 2026
@JasonCZMeng

Copy link
Copy Markdown
Contributor Author

Friendly ping — CI is green and no performance impact per CodSpeed. This is a minimal fix (replaces empty content with " " in ChatAnthropic, matching AutoGen's approach) for issue #35081 where empty message content causes crashes. Would love a review when you get a chance! 🙏

system.append(block)
else:
system.append(
{"type": "text", "text": " " if block == "" else block},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this can be written for better readability this
block_text = " " if block=="" else block
obj = {"type":"text", "text:block_text}
system.append(obj)

though this codes looks like a lot, but's more readable in my 2 cents. I let u decide than compacting the code

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@JasonCZMeng

Copy link
Copy Markdown
Contributor Author

Closing this to keep my PR queue focused — no reviewer activity in ~40 days. The change is still mergeable; happy to reopen if a maintainer wants to pick it up, or I'll revisit if the underlying issue resurfaces. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

anthropic `langchain-anthropic` package issues & PRs external fix For PRs that implement a fix integration PR made that is related to a provider partner package integration size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatAnthropic fails to handle empty HumanMessage content, leading to BadRequestError (400) from Anthropic API

2 participants