spring-projects / spring-projects/spring-ai

Response fails mid-stream when using Azure OpenAI endpoint due to asynchronous content filter

Open
#7,007 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Bug description
When using an OpenAiChatModel (spring-ai-openai) with an Azure OpenAI endpoint that has an asynchronous content filter, the response fails mid-stream because of an invalid chunk:

Caused by: com.openai.errors.OpenAIInvalidDataException: `delta` is not set
	at com.openai.core.JsonField.getRequired$openai_java_core(Values.kt:174)
	at com.openai.models.chat.completions.ChatCompletionChunk$Choice.delta(ChatCompletionChunk.kt:617)
	at org.springframework.ai.openai.OpenAiChatModel$ChunkMerger.hasToolCall(OpenAiChatModel.java:1183)
	at org.springframework.ai.openai.OpenAiChatModel.lambda$internalStream$6(OpenAiChatModel.java:319)
	at reactor.core.publisher.FluxPeekFuseable$PeekConditionalSubscriber.onNext(FluxPeekFuseable.java:836)
	at reactor.core.publisher.FluxCreate$BufferAsyncSink.drain(FluxCreate.java:887)
	at reactor.core.publisher.FluxCreate$BufferAsyncSink.next(FluxCreate.java:812)
	at reactor.core.publisher.FluxCreate$SerializedFluxSink.next(FluxCreate.java:164)
        ...

This is because Azure for some reason chooses to ignore the spec in this case and sends a chunk with no delta.

Mildly related:
https://github.com/spring-projects/spring-ai/issues/2691

Related change in the Python SDK:
https://github.com/openai/openai-python/pull/2255

Example chunk:
https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/content-streaming?view=foundry-classic#annotation-message

I honestly was unsure whether to create the issue here or with the official Java SDK, but I opted here because the Java SDK has APIs to determine the validity of a chunk, and thus seems to explicitly tolerate invalid objects. Ideally, Azure would fix it, of course.

Environment
Spring AI 2.0.1

Steps to reproduce
Use the ChatModel with Azure OpenAI and an asynchronouse filter and the streaming API, and prompt it for a response that is long enough for the error to surface. (A few paragraphs in)

Expected behavior
Stream should not fail.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in OpenAiChatModel.java at ChunkMerger.hasToolCall (around line 1183) and follow the internalStream path where the invalid Azure chunk reaches the merger. Reproduce with an Azure OpenAI endpoint using asynchronous content filtering and a sufficiently long streaming response. Done means a chunk without delta no longer causes the stream to fail.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.