Skip to content

fix(anthropic): parse non-streaming responses (H-04)#40

Merged
euxaristia merged 2 commits into
Cairn:mainfrom
PierrunoYT:fix-h04-anthropic-nonstream-parser
Jul 23, 2026
Merged

fix(anthropic): parse non-streaming responses (H-04)#40
euxaristia merged 2 commits into
Cairn:mainfrom
PierrunoYT:fix-h04-anthropic-nonstream-parser

Conversation

@PierrunoYT

Copy link
Copy Markdown
Collaborator

Summary

  • parse non-streaming Anthropic Messages API JSON through dedicated typed response, content-block, and usage representations
  • return provider-neutral text, thinking, and tool-use messages for print mode and compaction while leaving SSE streaming parsing unchanged
  • propagate input, output, cache-read, and cache-creation usage and reject Anthropic error envelopes
  • add regression tests for text, tool use, usage, errors, and retained streaming behavior

Testing

  • cargo test --locked llm::anthropic::tests -- --test-threads=1 (10 passed)
  • cargo test --locked --all-targets -- --test-threads=1 (227 passed)
  • git diff --check upstream/main...HEAD

Fixes #18

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Anthropic completion handling now uses separate parsers for streaming SSE events and non-streaming JSON responses. Non-streaming parsing extracts text, tool use, thinking, usage, cache tokens, and API errors, with expanded tests.

Changes

Anthropic response parsing

Layer / File(s) Summary
Non-streaming response parser
src/llm/anthropic.rs
Adds JSON response types and parsing for text, tool-use, thinking, usage, cache tokens, and Anthropic API errors, with corresponding tests.
Dedicated parser wiring
src/llm/anthropic.rs
Routes complete through the non-streaming parser and stream_complete through the streaming parser; updates streaming parser tests.

Estimated code review effort: 4 (Complex) | ~45 minutes

Suggested reviewers: euxaristia

Poem

A rabbit parsed the JSON bright,
While SSE hopped along its flight.
Text and tools joined usage too,
Cache tokens counted in the queue.
API errors now speak clear—
“No empty answers hiding here!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main change: fixing Anthropic non-streaming response parsing.
Description check ✅ Passed The description matches the code changes and tests around Anthropic non-streaming parsing.
Linked Issues check ✅ Passed The changes implement the dedicated non-streaming parser and usage handling requested in #18.
Out of Scope Changes check ✅ Passed The diff stays focused on Anthropic parsing and regression tests, with no clear unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@PierrunoYT

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
src/llm/anthropic.rs (1)

550-564: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Default missing Anthropic non-streaming token counts to 0 for consistency.

Anthropic non-streaming usage.input_tokens/usage.output_tokens are normally present, but unlike the streaming parser, this complete-response path turns a missing/invalid usage object or count into a hard parse error after content has already been parsed. Default these token counts to 0 like Usage::default() to keep empty usage from discarding otherwise-usable responses.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/llm/anthropic.rs` around lines 550 - 564, Update the non-streaming
Anthropic response parsing around the usage extraction in the
response-construction path to tolerate missing or invalid usage data. Default
both input_tokens and output_tokens to 0, including when the usage object is
absent, while preserving the parsed role and content instead of returning an
error.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/llm/anthropic.rs`:
- Around line 550-564: Update the non-streaming Anthropic response parsing
around the usage extraction in the response-construction path to tolerate
missing or invalid usage data. Default both input_tokens and output_tokens to 0,
including when the usage object is absent, while preserving the parsed role and
content instead of returning an error.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c4737792-4120-465f-b90c-81c9448b7e1a

📥 Commits

Reviewing files that changed from the base of the PR and between 0304f80 and faeb723.

📒 Files selected for processing (1)
  • src/llm/anthropic.rs

@euxaristia
euxaristia merged commit a6f2fbe into Cairn:main Jul 23, 2026
4 checks passed
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.

[High][H-04] Parse Anthropic non-streaming responses correctly

2 participants