Skip to content

Cast message to MessageType before creating StreamChunk in stream_broadcaster#311

Open
TheRealNeil wants to merge 1 commit intoactiveagents:mainfrom
qlarity:fix-chunk-message-content
Open

Cast message to MessageType before creating StreamChunk in stream_broadcaster#311
TheRealNeil wants to merge 1 commit intoactiveagents:mainfrom
qlarity:fix-chunk-message-content

Conversation

@TheRealNeil
Copy link
Contributor

@TheRealNeil TheRealNeil commented Feb 8, 2026

Cast message to MessageType before creating StreamChunk in stream_broadcaster

Fixes #307

Summary

  • Cast raw Hash messages to MessageType in stream_broadcaster so that chunk.message has the same typed interface as response.message

Root cause

During streaming, StreamChunk#message wraps the raw Hash from the provider's message stack — no type casting is applied. After generation completes, PromptResponse#messages are cast through Types::MessagesType into Common::Messages::* objects with method accessors (.content, .role).

This means chunk.message.content raises NoMethodError during streaming, while response.message.content works fine after generation. Users have to use chunk.message[:content] during streaming but response.message.content afterwards — an inconsistent API.

Fix

In stream_broadcaster, cast the message through Providers::Common::Messages::Types::MessageType when it's a Hash before passing it to StreamChunk.new. This gives chunk.message the same typed interface as response.message, so .content and .role work consistently in both streaming and non-streaming contexts.

Test plan

  • Verify chunk.message.content works in on_stream callbacks
  • Verify chunk.message.role works in on_stream callbacks
  • Confirm non-Hash messages are passed through unchanged
  • Confirm post-generation response.message.content is unaffected

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.

StreamChunk.message is a raw Hash while PromptResponse.message is a typed object

1 participant