Skip to content

Conversation

kuntal1461
Copy link
Contributor

Summary

This PR fixes an issue where ChatClient.content() returned only the first generation when multiple tools used returnDirect = true.
The update ensures that all returnDirect generations are aggregated consistently across both call() and stream() flows, independent of the provider implementation.


Changes

File updated:
spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/DefaultChatClient.java

  • getContentFromChatResponse (around L525):

    • Single generation: Behavior unchanged.
    • Multiple generations with finishReason = "returnDirect":
      → Concatenate all generation texts using a newline (\n).
    • Otherwise: Fall back to the first generation (existing behavior).
  • stream().content() aggregation logic:
    Updated to mirror the same multi-generation handling for consistency with synchronous responses.


Tests

New test class:
spring-ai-client-chat/src/test/java/org/springframework/ai/chat/client/DefaultChatClientReturnDirectAggregationTests.java

  • aggregatesMultipleReturnDirectGenerationsInContent:
    Verifies that multiple returnDirect generations are joined with newlines.
  • returnsFirstWhenNotAllReturnDirect:
    Ensures fallback to the first generation when not all generations use returnDirect.

Compatibility

No breaking changes.

  • Behavior remains identical when:

    • There is only one generation, or
    • Not all generations are marked returnDirect.

This enhancement improves content consistency and tool aggregation behavior without altering existing APIs.


Notes

  • Ensures unified aggregation logic across both call() and stream() methods.
  • Keeps provider implementations untouched and fully backward-compatible.

…ultChatClient (spring-projects#4655)

Signed-off-by: Kuntal Maity <kuntal.1461@gmail.com>
…ultChatClient (spring-projects#4655)

Signed-off-by: Kuntal Maity <kuntal.1461@gmail.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.

1 participant