Skip to content

fix: prevent IllegalStateException when ToolResponseMessage has no responses (GH-6197)#6213

Open
suryateja-g13 wants to merge 1 commit into
spring-projects:mainfrom
suryateja-g13:gh-6197-openai-tool-response-toolcallid
Open

fix: prevent IllegalStateException when ToolResponseMessage has no responses (GH-6197)#6213
suryateja-g13 wants to merge 1 commit into
spring-projects:mainfrom
suryateja-g13:gh-6197-openai-tool-response-toolcallid

Conversation

@suryateja-g13
Copy link
Copy Markdown
Contributor

Problem

With the new OpenAI Java SDK, ChatCompletionToolMessageParam.builder().build() now throws IllegalStateException: toolCallId is required at build time if toolCallId is not set. The previous code had two bugs that trigger this:

  1. Empty responses path: when ToolResponseMessage.getResponses() is empty, the old code called builder.build() without ever setting toolCallId.
  2. Builder reuse: a single builder instance was shared across all responses in the stream, meaning state from a previous response (e.g. a null ID) could bleed into the next.

The ToolCallAdvisor introduced in M7 auto-registers when tools are configured, changing the tool call flow and exposing both of these latent bugs.

Fix

  • Stream through toolMessage.getResponses(), skipping entries where id is null/empty.
  • Create a fresh ChatCompletionToolMessageParam.Builder per response to eliminate builder-reuse state leakage.
  • Remove the separate empty-responses early-return branch (now handled naturally by the filtered stream returning an empty list).

Tests

Three new unit tests added to OpenAiChatModelTests:

  • toolResponseMessageWithPopulatedResponses_mapsToOneParamPerResponse — verifies each response maps to one tool message param.
  • toolResponseMessageWithEmptyResponses_producesNoMessages — verifies an empty ToolResponseMessage produces zero tool-typed params.
  • toolResponseMessageWithNullId_skipsResponse — verifies responses with null IDs are filtered out.

Fixes #6197

…sponses

When ToolResponseMessage.getResponses() is empty or contains null IDs,
the previous code attempted to build a ChatCompletionToolMessageParam
without setting the required toolCallId field, causing the new OpenAI
Java SDK to throw IllegalStateException at build time.

Fix: stream responses, skip entries with null/empty IDs, and create a
fresh builder per response to avoid builder state leaking across items.

Fixes spring-projects#6197

Signed-off-by: Gorre Surya <suryateja.g13@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.

IllegalStateException: toolCallId is required when using @Tool or .tools() with OpenAI in version 2.0.0-M7

1 participant