Skip to content

Omit blank response text from Anthropic message history - #271

Merged
mattt merged 1 commit into
mainfrom
mattt/skip-empty-anthropic-text
Sep 27, 2026
Merged

mattt merged 1 commit into
mainfrom
mattt/skip-empty-anthropic-text

Conversation

@mattt

@mattt mattt commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

When a model calls a tool and writes no text, the session records an empty response. On the next turn, AnthropicLanguageModel sends that response as an assistant message with an empty text block. The Anthropic API rejects text blocks that are empty or contain only whitespace with a 400 error, so every later request in the session fails. Copilot found this while reviewing #264.

This PR drops those text blocks from response entries when building the message history, and omits the assistant message when no content remains. It also adds a request-encoding test for an empty and a whitespace-only response after a tool call.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

No unresolved review issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Updates Anthropic message history encoding to omit empty or whitespace-only assistant responses after tool calls, preventing API request failures.

Changes:

  • Filters blank response text blocks.
  • Omits assistant messages with no remaining content.
  • Adds request-encoding tests for empty and whitespace-only responses.
File Description
Tests/​AnyLanguageModelTests/​AnthropicLanguageModelTests.swift Tests blank-response history encoding.
Sources/​AnyLanguageModel/​Models/​AnthropicLanguageModel.swift Filters invalid response content during history conversion.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mattt
mattt merged commit 0c4b24d into main Sep 27, 2026
13 checks passed
@mattt
mattt deleted the mattt/skip-empty-anthropic-text branch September 27, 2026 11:02
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.

2 participants