Omit blank response text from Anthropic message history - #271
Merged
Merged
Conversation
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a model calls a tool and writes no text, the session records an empty response. On the next turn,
AnthropicLanguageModelsends 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.