Skip to content

Conversation

@cpsievert
Copy link
Collaborator

Summary

Port of tidyverse/ellmer#909

This PR adds a stream_content() method to providers that returns Content objects (ContentText or ContentThinking) instead of strings. This allows downstream consumers like shinychat to distinguish between content types during streaming and provide specific UI for thinking content.

Changes

  • Add abstract stream_content() method to Provider base class that returns Optional[Content]
  • Change stream_text() from abstract to concrete, delegating to stream_content() and extracting text
  • Update all providers to implement stream_content():
    • Anthropic: Returns ContentText for text deltas, ContentThinking for thinking deltas
    • OpenAI (Responses API): Returns ContentText for text, ContentThinking for reasoning summary
    • Google: Returns ContentText or ContentThinking based on the thought flag
    • OpenAI Completions: Returns ContentText
    • Snowflake: Returns ContentText
  • Add thinking content support to Google provider's _as_turn() method

Backward Compatibility

The Chat class continues to work unchanged because:

  • It still calls stream_text()
  • stream_text() internally calls stream_content() and extracts the text
  • Downstream consumers can now call stream_content() directly if they need to distinguish content types

Test plan

  • All existing provider tests pass (73 tests)
  • Type checking passes
  • Lint checks pass

🤖 Generated with Claude Code

cpsievert and others added 3 commits January 13, 2026 20:37
Add stream_content() method to providers that returns Content objects
(ContentText or ContentThinking) instead of strings. This allows
downstream consumers like shinychat to distinguish between content
types during streaming.

- Add abstract stream_content() method to Provider base class
- Change stream_text() to delegate to stream_content()
- Update all providers to implement stream_content()
- Add thinking content support to Google provider's _as_turn()

Port of tidyverse/ellmer#909

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move the [empty string] conversion from __init__ to __str__ in ContentText
so that whitespace-only text is preserved internally while still displaying
nicely. This fixes streaming tests where newlines between words were being
converted to [empty string].

Also revert the .isspace() filtering in stream_content() methods since
ContentText now preserves the original text.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Revert ContentText change and instead filter empty/whitespace in
stream_content() methods. This preserves the existing API serialization
behavior (where "[empty string]" is used) while avoiding yielding
"[empty string]" during streaming.

Update test_simple_streaming_chat_async to not require newlines in
streamed output since whitespace chunks are now filtered.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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