Skip to content

openai stream Validation issues #1358

@apetoo

Description

@apetoo

openai stream

response:
{"id":"chatcmpl-msg_bdrk_012bpm3yfa9inEuftTWYQ46F","object":"chat.completion.chunk","created":1726239401,"model":"claude-3-5-sonnet-20240620","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":""}]}

Because finish_reason is an empty string, the conversion is abnormal, there is something wrong with the check, it is not strict enough, and ChatCompletionFinishReason should use NON_EMPTY
`
public static T jsonToObject(String json, Class type) {
try {
return OBJECT_MAPPER.readValue(json, type);
}
catch (Exception e) {
throw new RuntimeException("Failed to json: " + json, e);
}
}

@JsonInclude(Include.NON_NULL)
public record ChunkChoice(// @Formatter:off
@JsonProperty("finish_reason") ChatCompletionFinishReason finishReason,
@JsonProperty("index") Integer index,
@JsonProperty("delta") ChatCompletionMessage delta,
@JsonProperty("logprobs") LogProbs logprobs) {// @Formatter:on
}

// finish_reason is null, and the parsing is correct:

public static void main(String[] args) {
    String str = "{\"id\":\"chatcmpl-msg_bdrk_012bpm3yfa9inEuftTWYQ46F\",\"object\":\"chat.completion.chunk\",\"created\":1726239401,\"model\":\"claude-3-5-sonnet-20240620\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"},\"finish_reason\":null}]}";
    OpenAiApi.ChatCompletionChunk chatCompletionChunk = ModelOptionsUtils.jsonToObject(str, OpenAiApi.ChatCompletionChunk.class);
    System.out.println(chatCompletionChunk);
}

`

image
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions