Skip to content

Conversation

@GALCF
Copy link

@GALCF GALCF commented Jan 23, 2026

Description

Hi prism! Great package, I love the way you structured so many providers into one approachable framework.

I recently discovered that when streaming text, tool calls - combined with a low max steps value - do not respond with a StreamEndEvent.
This is problematic in my case because only the StreamEndEvent tracks usage data.

I adapted all providers to send a StreamEndEvent when reaching max steps.
It seems DeepSeek, Groq, Mistral and XAI handled tool calls diffently, they threw "Maximum tool call chain depth exceeded" instead of stopping gracefully.
I adapted those as well and wrote tests for all providers, but I only have access to test Anthropic, Gemini, Ollama, OpenAI and Mistral with real credentials.

Feel free to ask questions, make or request changes.

Test code

I tested real credentials with the code below

$events = \prism()
    ->text()
    ->using('provider', 'model')
    ->withPrompt('First, add 2+2. Next add 3+5. Finally, add the two results together.')
    ->withTools([
        Tool::as('adder')
            ->for('Adds two numbers together.')
            ->withNumberParameter('first', 'The first number to add.')
            ->withNumberParameter('second', 'The second number to add.')
            ->using(static fn (float $first, float $second) => (string) ($first + $second)),
    ])
    ->withMaxSteps(1)
    ->withToolChoice('adder')
    ->asStream()
;

foreach ($events as $event) {
    echo $event::class . '<br>';
    \ob_flush();
    \flush();
}

Results

OpenAI (gpt-4.1-mini):

Prism\Prism\Streaming\Events\StreamStartEvent
Prism\Prism\Streaming\Events\StepStartEvent
Prism\Prism\Streaming\Events\ToolCallDeltaEvent (x9)
Prism\Prism\Streaming\Events\ToolCallEvent
Prism\Prism\Streaming\Events\ToolResultEvent
Prism\Prism\Streaming\Events\StepFinishEvent
> No StreamEndEvent

Gemini (gemini-2.5-flash):

Prism\Prism\Streaming\Events\StreamStartEvent
Prism\Prism\Streaming\Events\StepStartEvent
Prism\Prism\Streaming\Events\ToolCallEvent
Prism\Prism\Streaming\Events\ToolResultEvent
Prism\Prism\Streaming\Events\StepFinishEvent
> No StreamEndEvent

Anthropic (claude-haiku-4-5-20251001):

Prism\Prism\Streaming\Events\StreamStartEvent
Prism\Prism\Streaming\Events\StepStartEvent
Prism\Prism\Streaming\Events\ToolCallDeltaEvent (x6)
Prism\Prism\Streaming\Events\ToolCallEvent
Prism\Prism\Streaming\Events\ToolCallDeltaEvent (x7)
Prism\Prism\Streaming\Events\ToolCallEvent
Prism\Prism\Streaming\Events\ToolResultEvent (x2)
Prism\Prism\Streaming\Events\StepFinishEvent
> No StreamEndEvent

Mistral (via Google Vertex AI) (mistral-small-2503):

Prism\Prism\Streaming\Events\StreamStartEvent
Prism\Prism\Streaming\Events\StepStartEvent
Prism\Prism\Streaming\Events\ToolCallEvent (x2)
Prism\Prism\Streaming\Events\ToolResultEvent (x2)
Prism\Prism\Streaming\Events\StepFinishEvent
> PrismException: Maximum tool call chain depth exceeded

Breaking Changes

Text Streaming now emits StreamEndEvent after StepFinishEvent of tool calls

GALCF added 10 commits January 24, 2026 09:42
before: usage was collected only when the stream ended without tool calls & max steps reached

after: usage is always collected
given: tool calls were handled and max steps is reached

before: thrown PrismException with message "Maximum tool call chain depth exceeded"

after: stream ends gracefully with StreamEndEvent
given: tool calls were handled and max steps is reached

before: thrown PrismException with message "Maximum tool call chain depth exceeded"

after: stream ends gracefully with StreamEndEvent
given: tool calls were handled and max steps is reached

before: thrown PrismException with message "Maximum tool call chain depth exceeded"

after: stream ends gracefully with StreamEndEvent
given: tool calls were handled and max steps is reached

before: thrown PrismException with message "Maximum tool call chain depth exceeded"

after: stream ends gracefully with StreamEndEvent
@GALCF GALCF force-pushed the fix/tool-call-stream-end branch from f0723ec to 9c5a47f Compare January 24, 2026 08:47
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.

1 participant