Skip to content

DRAFT: Streaming support#23

Draft
axyr wants to merge 3 commits intoprism-php:mainfrom
axyr:main
Draft

DRAFT: Streaming support#23
axyr wants to merge 3 commits intoprism-php:mainfrom
axyr:main

Conversation

@axyr
Copy link
Copy Markdown

@axyr axyr commented May 30, 2025

I have minimal viable stream working, but only tested with anthropic.claude-3-5-sonnet-20240620-v1:0.

It is far from complete, and it was a bit harder than I expected...

I'm still finding my way around the different schema's and message formats.

I tried to keep things as 'Prism' as possible, but to get the stream even decoding working I had to use the aws sdk client in order to retrieve readable events from converseStream.

Added this WIP PR for first feedback and to 'publish' the work to prevent doing double work if others are also working on this.

        $stream = Prism::text()
            ->using(Bedrock::KEY, env('ANTHROPIC_MODEL'))
            ->withPrompt('Hi, how are you?')
            ->asStream();

        foreach ($stream as $chunk) {
            $this->line($chunk->text);
        }
image

Comment thread src/HandlesStream.php

protected function handleMessageStart(array $chunk): null
{
// {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure how to best store this information.

Comment thread src/HandlesStream.php
);
}

protected function handleReasoningContentBlockDelta(array $reasoningContent): Chunk
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is not tested yet.

Comment thread src/HandlesStream.php
);
}

protected function handleMetadata(array $chunk): void
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure what to do with this information.

'version' => config('prism.providers.bedrock.version', 'latest'),
'credentials' => [
'key' => config('prism.providers.bedrock.api_key', ''),
'secret' => config('prism.providers.bedrock.api_secret', ''),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you'll need to add 'token' => config('prism.providers.bedrock.session_token', ''), to work with sts.

* 'main' of github.com:prism-php/bedrock:
  fix(converse): parameterless tools (prism-php#28)
  fix: prism >=v0.77.1 incompatibilities (prism-php#27)

# Conflicts:
#	src/Bedrock.php
DanielFerguson added a commit to communitilabs/prism-bedrock that referenced this pull request Apr 18, 2026
Adds stream() implementation for the Converse schema via the AWS SDK's
BedrockRuntimeClient::converseStream() — the binary event-stream format
returned by Bedrock is non-trivial to decode over raw HTTP, and the SDK
already does it for us.

- New BedrockStreamHandler contract mirroring the Text/Structured handlers
- New ConverseStreamHandler implementing the streaming + tool-use loop,
  using Prism's CallsTools trait for multi-step recursion up to maxSteps
- BedrockSchema gains streamHandler() (Converse only for now; Anthropic
  schema streaming is future work)
- Bedrock provider exposes awsCredentials() / awsRegion() so the stream
  handler can construct an SDK client; stream() dispatches via the schema
  like text() and structured()

Maps the full Converse stream event vocabulary (contentBlockStart /
contentBlockDelta / messageStop / metadata) into Prism's typed StreamEvent
subclasses (StreamStart, StepStart, TextStart/Delta/Complete, ToolCall,
StepFinish, StreamEnd).

Tested end-to-end against ap-southeast-2 with the au.* APAC inference
profile in a downstream app — both streaming text and streaming tool-use
loops verified. Integration-style coverage that drives converseStream()
through a mocked AWS SDK client is the natural follow-up; draft PR prism-php#23
establishes the BedrockClientFactory + MockHandler pattern we'd reuse.
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