Skip to content

Tools are called twice on streaming requests for Anthropic APIΒ #1370

@youssefwadie

Description

@youssefwadie

Bug description

When using the Anthropic API in Spring AI with streaming responses, tools (functions) are called twice. This behavior does not occur in blocking calls (i.e., ChatClient.call), where each function is invoked only once, as expected.

Environment

  • Spring Boot version: 3.3.3
  • Spring AI version: 1.0.0-M2
  • Java version: 17
  • OS: Windows 11

Steps to reproduce

  1. Register a custom function using the Spring AI Anthropic API for a streaming request.
  2. Trigger a request that involves streaming function calling.
  3. Observe that the function is invoked twice during the stream, even though it should only be triggered once.

Expected behavior
The function should be called only once during the streaming response, just like the behavior observed in blocking calls.

Minimal Complete Reproducible example
I have replicated Dan Vega's example from his "Spring into AI" tutorial, which you can find here: Dan Vega's Spring into AI example.
But, I replaced the OpenAI client with Anthropic's API. You can find my version here: Spring AI Anthropic Function Calling.

The logs for both streaming and non-streaming requests demonstrate the issue. The tool is invoked twice in the streaming scenario but only once in the non-streaming scenario, as expected.

Non-streaming request
Non streaming


Streaming request
Streaming

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions