From 2e623c7cea7cb48fbf8706f2856fe7fc0eb6ac8d Mon Sep 17 00:00:00 2001 From: Alex Baranov <677093+sashabaranov@users.noreply.github.com> Date: Fri, 11 Jul 2025 20:04:15 +0100 Subject: [PATCH 1/2] check error when unmarshalling ExtraBody --- embeddings.go | 5 ++++- embeddings_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/embeddings.go b/embeddings.go index 8593f8b5b..e91820fa2 100644 --- a/embeddings.go +++ b/embeddings.go @@ -268,7 +268,10 @@ func (c *Client) CreateEmbeddings( // Deserialize JSON to map[string]any var body map[string]any - _ = json.Unmarshal(jsonData, &body) + err = json.Unmarshal(jsonData, &body) + if err != nil { + return + } req, err := c.newRequest( ctx, diff --git a/embeddings_test.go b/embeddings_test.go index 07f1262cb..7212e2755 100644 --- a/embeddings_test.go +++ b/embeddings_test.go @@ -15,6 +15,13 @@ import ( "github.com/sashabaranov/go-openai/internal/test/checks" ) +// badMarshaler produces invalid JSON when marshaled. +type badMarshaler struct{} + +func (badMarshaler) MarshalJSON() ([]byte, error) { + return []byte("{"), nil +} + func TestEmbedding(t *testing.T) { embeddedModels := []openai.EmbeddingModel{ openai.AdaSimilarity, @@ -325,3 +332,21 @@ func TestDotProduct(t *testing.T) { t.Errorf("Expected Vector Length Mismatch Error, but got: %v", err) } } + +// TestCreateEmbeddings_UnmarshalError verifies that an error is returned when +// the JSON bytes produced by marshaling the request cannot be unmarshaled back +// into a map. +func TestCreateEmbeddings_UnmarshalError(t *testing.T) { + client, server, teardown := setupOpenAITestServer() + defer teardown() + + server.RegisterHandler("/v1/embeddings", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + fmt.Fprint(w, `{"data":[]}`) + }) + + _, err := client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{ + Input: badMarshaler{}, + }) + checks.HasError(t, err, "CreateEmbeddings should fail on unmarshal error") +} From 74a7c864c18f7c1136e2a9a29e38a066d6f018fd Mon Sep 17 00:00:00 2001 From: Alex Baranov <677093+sashabaranov@users.noreply.github.com> Date: Sat, 12 Jul 2025 08:10:38 +0100 Subject: [PATCH 2/2] Add error handling for request body unmarshalling --- embeddings.go | 3 +-- embeddings_test.go | 25 ------------------------- 2 files changed, 1 insertion(+), 27 deletions(-) diff --git a/embeddings.go b/embeddings.go index e91820fa2..12a9d3ec5 100644 --- a/embeddings.go +++ b/embeddings.go @@ -268,8 +268,7 @@ func (c *Client) CreateEmbeddings( // Deserialize JSON to map[string]any var body map[string]any - err = json.Unmarshal(jsonData, &body) - if err != nil { + if err = json.Unmarshal(jsonData, &body); err != nil { return } diff --git a/embeddings_test.go b/embeddings_test.go index 7212e2755..07f1262cb 100644 --- a/embeddings_test.go +++ b/embeddings_test.go @@ -15,13 +15,6 @@ import ( "github.com/sashabaranov/go-openai/internal/test/checks" ) -// badMarshaler produces invalid JSON when marshaled. -type badMarshaler struct{} - -func (badMarshaler) MarshalJSON() ([]byte, error) { - return []byte("{"), nil -} - func TestEmbedding(t *testing.T) { embeddedModels := []openai.EmbeddingModel{ openai.AdaSimilarity, @@ -332,21 +325,3 @@ func TestDotProduct(t *testing.T) { t.Errorf("Expected Vector Length Mismatch Error, but got: %v", err) } } - -// TestCreateEmbeddings_UnmarshalError verifies that an error is returned when -// the JSON bytes produced by marshaling the request cannot be unmarshaled back -// into a map. -func TestCreateEmbeddings_UnmarshalError(t *testing.T) { - client, server, teardown := setupOpenAITestServer() - defer teardown() - - server.RegisterHandler("/v1/embeddings", func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(http.StatusOK) - fmt.Fprint(w, `{"data":[]}`) - }) - - _, err := client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{ - Input: badMarshaler{}, - }) - checks.HasError(t, err, "CreateEmbeddings should fail on unmarshal error") -}