Skip to content

feat: use StreamingChunk.reasoning field for Anthropic thinking content#2849

Merged
anakin87 merged 4 commits intodeepset-ai:mainfrom
yaowubarbara:fix/anthropic-streaming-reasoning
Feb 18, 2026
Merged

feat: use StreamingChunk.reasoning field for Anthropic thinking content#2849
anakin87 merged 4 commits intodeepset-ai:mainfrom
yaowubarbara:fix/anthropic-streaming-reasoning

Conversation

@yaowubarbara
Copy link
Contributor

@yaowubarbara yaowubarbara commented Feb 17, 2026

Related Issues

Proposed Changes:

Right now the Anthropic integration dumps the entire stream event into StreamingChunk.meta via chunk.model_dump(), which means thinking/reasoning content ends up buried in meta["delta"]["thinking"] instead of being accessible through the dedicated StreamingChunk.reasoning field that was added in Haystack 2.18.0.

This PR updates _convert_anthropic_chunk_to_streaming_chunk to populate StreamingChunk.reasoning with a proper ReasoningContent object when it encounters thinking-related events:

  • content_block_start with type thinking or redacted_thinking
  • content_block_delta with type thinking_delta or signature_delta

Also updated _process_reasoning_contents to read from chunk.reasoning instead of digging through chunk.meta, which simplifies the logic quite a bit. Extracted the repeated group-finalization code into a small helper (_finalize_reasoning_group).

The raw event data is still available in meta for anyone who needs it -- this just makes reasoning content properly accessible through the structured field.

How did you test it?

  • Updated existing unit tests to assert that StreamingChunk.reasoning is populated correctly for thinking block start, thinking delta, and signature delta events
  • All existing tests still pass

Notes for the reviewer

The non-streaming path (_convert_chat_completion_to_chat_message) already uses ReasoningContent correctly, so no changes needed there. This only affects the streaming code path.

Checklist

@yaowubarbara yaowubarbara requested a review from a team as a code owner February 17, 2026 14:23
@yaowubarbara yaowubarbara requested review from anakin87 and removed request for a team February 17, 2026 14:23
@CLAassistant
Copy link

CLAassistant commented Feb 17, 2026

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added integration:anthropic type:documentation Improvements or additions to documentation labels Feb 17, 2026
Migrate Anthropic streaming to use the dedicated reasoning field
instead of storing thinking content in StreamingChunk.meta.
@anakin87
Copy link
Member

@yaowubarbara, thanks for the contribution.

  1. Please sign the CLA
  2. Before opening more PRs on this topic, let's first align on the approach

@yaowubarbara
Copy link
Contributor Author

Thanks for the review! I've signed the CLA.

Just a heads-up — I opened #2850 for the Ollama side before seeing your comment. Happy to hold off on any further PRs until we're aligned on the approach here.

Let me know if there's anything you'd like me to change, or if you have a different direction in mind for the reasoning field migration.

@anakin87
Copy link
Member

Thank you. I'll take a look...

Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

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

I left some initial comments.

- Restore mypy comment for reasoning_text type inference
- Add parameter descriptions to _finalize_reasoning_group docstring
- Add unit tests for _finalize_reasoning_group
- Add check to skip chunks with empty reasoning content
Signature-only chunks now accumulate without triggering new content
groups, matching the previous behavior more closely.
Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

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

Thank you. Looks good

@anakin87 anakin87 merged commit 9dcdaee into deepset-ai:main Feb 18, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:anthropic type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic - use reasoning field in StreamingChunk

3 participants