Fixing Deserialization Failure for Enum with Empty String finish_reason

Open Beginner friendly
#2,645 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
java
Domain
api

Research direction

Start at org.springframework.ai.openai.api.OpenAiApi$ChatCompletionFinishReason and reproduce the reported ChatCompletionChunk JSON with an empty finish_reason. Confirm that the enum deserializes both an empty string and null without the InvalidFormatException; the issue is done when the provided payload can be parsed normally.

Written by the indexing model from the issue text.

Description

status: waiting-for-triage

Bug description
Deserialization of org.springframework.ai.openai.api.OpenAiApi$ChatCompletionFinishReason fails when the finish_reason field is an empty string "" rather than null during the step-by-step inference process. For instance, while the model is inferring step-by-step, the finish_reason field typically returns an empty string "" or null until all tokens are returned. Due to the following enum definition, "" cannot be directly handled:
public static enum ChatCompletionFinishReason {
@JsonProperty("stop")
STOP,
@JsonProperty("length")
LENGTH,
@JsonProperty("content_filter")
CONTENT_FILTER,
@JsonProperty("tool_calls")
TOOL_CALLS,
@JsonProperty("tool_call")
TOOL_CALL;

    private ChatCompletionFinishReason() {
    }
}

Environment
Please provide as many details as possible: Spring AI version, Java version, which vector store you use if any, etc
1.0.0-M6
Steps to reproduce
Steps to reproduce the issue.

Expected behavior
A clear and concise description of what you expected to happen.
when finish_reason is "",it can be deserialized normally

Minimal Complete Reproducible example
Please provide a failing test or a minimal complete verifiable example that reproduces the issue.
Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot coerce empty String ("") to org.springframework.ai.openai.api.OpenAiApi$ChatCompletionFinishReason value (but could if coercion was enabled using CoercionConfig)
at [Source: (String)"{"id":"xxxxxx","object":"chat.completion.chunk","created":1742218321,"model":"xxxx","choices":[{"index":0,"delta":{"role":"assistant","content":"","tool_calls":null,"reasoning_content":null},"logprobs":null,"finish_reason":"","matched_stop":null}],"usage":null}"; line: 1, column: 257] (through reference chain: org.springframework.ai.openai.api.OpenAiApi$ChatCompletionChunk["choices"]->java.util.ArrayList[0]->org.springframework.ai.openai.api.OpenAiApi$ChatCompletionChunk$ChunkChoice["finish_reason"])

Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 10h
Merged PRs (30d)
5

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.

More from spring-projects/spring-ai

All issues in spring-projects/spring-ai

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.