Skip to content

tthe index value is negative, violating the ensureNotNegative validation rule and causing the program to crash. #203

@tingxin

Description

@tingxin

Describe the bug
[dev.langchain4j.internal.Exceptions.illegalArgument(Exceptions.java:23), dev.langchain4j.internal.ValidationUtils.ensureNotNegative(ValidationUtils.java:195), dev.langchain4j.model.chat.response.PartialToolCall.(PartialToolCall.java:27), dev.langchain4j.model.chat.response.PartialToolCall$Builder.build(PartialToolCall.java:122), dev.langchain4j.model.openai.OpenAiStreamingChatModel.handle(OpenAiStreamingChatModel.java:223), dev.langchain4j.model.openai.OpenAiStreamingChatModel.lambda$doChat$0(OpenAiStreamingChatModel.java:152), dev.langchain4j.model.openai.internal.StreamingRequestExecutor$2.onEvent(StreamingRequestExecutor.java:112), dev.langchain4j.http.client.log.LoggingHttpClient$1.onEvent(LoggingHttpClient.java:79), dev.langchain4j.http.client.sse.DefaultServerSentEventParser.lambda$parse$0(DefaultServerSentEventParser.java:25), dev.langchain4j.http.client.sse.ServerSentEventListenerUtils.ignoringExceptions(ServerSentEventListenerUtils.java:14), dev.langchain4j.http.client.sse.DefaultServerSentEventParser.parse(DefaultServerSentEventParser.java:25), dev.langchain4j.http.client.spring.restclient.SpringRestClient.lambda$execute$3(SpringRestClient.java:114), org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchangeInternal(DefaultRestClient.java:495), org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchange(DefaultRestClient.java:463), org.springframework.web.client.RestClient$RequestHeadersSpec.exchange(RestClient.java:565), dev.langchain4j.http.client.spring.restclient.SpringRestClient.lambda$execute$6(SpringRestClient.java:95), java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144), java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642), java.base/java.lang.Thread.run(Thread.java:1583)]

The core issue likely stems from an incompatibility between your proxy server and LangChain4j's OpenAiStreamingChatModel when handling streaming tool calls.

Here is a detailed analysis of the cause and several solutions for you.

Problem Root Cause Analysis

Error Stack Trace Interpretation:

  • dev.langchain4j.internal.ValidationUtils.ensureNotNegative: This error clearly indicates that a parameter which cannot be negative has been passed a negative value.
  • dev.langchain4j.model.chat.response.PartialToolCall.<init>: The error occurs when creating a PartialToolCall object, which is used to progressively build a complete tool call within a streaming response.
  • dev.langchain4j.model.openai.OpenAiStreamingChatModel.handle: The error originates from OpenAiStreamingChatModel processing the streaming data returned by the server.

Connecting the Clues:
When an OpenAI-compatible API performs streaming tool calls, it sends multiple tool_calls chunks within the data stream. Each chunk has an index field (e.g., {"index": 0}) to identify which tool call fragment it belongs to.
LangChain4j's OpenAiStreamingChatModel reads this index value and uses it to create or update PartialToolCall objects.
In your case, the index value is negative, violating the ensureNotNegative validation rule and causing the program to crash.

Why Does This Happen?
Your architecture is: LangChain4j (OpenAiStreamingChatModel) -> Proxy -> AWS Bedrock (Claude).
LangChain4j expects to receive streaming responses in a format that exactly matches the OpenAI API.
Your proxy server is responsible for converting AWS Bedrock's native streaming responses into an OpenAI-compatible format.
The problem lies in this conversion step. When the proxy transforms Bedrock's tool call streaming responses, it may generate an invalid index (such as -1) or omit the index entirely, causing LangChain4j's parser to fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions