AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ChatAnthropic Passes Empty HumanMessage Content To Anthropic API Causing 400 BadRequestError

When a HumanMessage with an empty string content is passed to ChatAnthropic, the request payload includes an invalid empty content block, and the Anthropic Messages API returns a 400 Bad Request. The LangChain integration should validate and filter empty content before sending the request.

highConfidence 85%Langchain-AnthropicVector StoresEmbeddingslangchain-core

Origin Analysis

The langchain-anthropic message conversion does not check for empty string or whitespace-only content, so it emits {'role':'user','content':''} (or an empty text block) to the Anthropic API. Anthropic requires non-empty text content, so the API rejects the request with a 400 status.
1. Set ANTHROPIC_API_KEY. 2. Instantiate ChatAnthropic with model 'claude-haiku-3', max_tokens=100, max_retries=0. 3. Create messages=[HumanMessage(content='', source='Alice')]. 4. Call llm.invoke(messages). 5. Observe anthropic.BadRequestError raised during /v1/messages POST.

Fixing Code Block

Edge Case Audit

This monkeypatch changes behavior by silently discarding empty messages; if an application expects an error for empty input, this may mask logic bugs. It also patches a private method and may break on internal refactors or future versions. Rollback: reassign ChatAnthropic._get_request_payload = original or restart the process. Test with multi-modal lists to ensure non-text blocks are retained. Under concurrent usage, the patch is process-global but thread-safe because it only wraps the call; no mutable shared state is introduced.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state