openai-go icon indicating copy to clipboard operation
openai-go copied to clipboard

When unmarshaling ChatCompletionChunk, the sdk doesn't check if the input is valid

Open spacewander opened this issue 7 months ago • 1 comments

Using the snippet below:

	chunk = openai.ChatCompletionChunk{}
	err := json.Unmarshal([]byte(`[]`), &chunk)
	fmt.Printf("%+v\n", err)

The err is nil and the chunk is successfully unmarshalled, though it will have empty value except that the raw field is [].

spacewander avatar Apr 23 '25 07:04 spacewander

Sometimes, errors are ignored during decoding: https://github.com/openai/openai-go/blob/c0414f15a9f4065adee2ed96a4dcd4d4cb9708aa/internal/apijson/decoder.go#L408-L411

https://github.com/openai/openai-go/blob/c0414f15a9f4065adee2ed96a4dcd4d4cb9708aa/internal/apijson/decoder.go#L430-L435

https://github.com/openai/openai-go/blob/c0414f15a9f4065adee2ed96a4dcd4d4cb9708aa/internal/apijson/decoder.go#L482-L486

What is the reason behind them?

spacewander avatar Apr 24 '25 02:04 spacewander