Repository: sashabaranov/go-openai
Pull request #899 (2024-11-14 08:26:08)

Make user field optional in embedding request
**Describe the change**
Added `omitempty` json tag for the `User` field in the `EmbeddingRequest` struct. This field is optional in embedding requests as per OpenAI spec.

Issue: If you don't specify any user in the embedding request, the current spec sends the user as an empty string for which embedding models throw errors.

**Provide OpenAI documentation link**
https://platform.openai.com/docs/api-reference/embeddings/create#embeddings-create-user

**Describe your solution**
Added `omitempty` json tag for the `User` field in the `EmbeddingRequest` struct. This will make the `User` field optional as per openai embeddings spec.

**Tests**
Tested the above change by sending embeddings requests to the embeddings model with following payload:
```
curl -k -X 'POST' '/embeddings' \
 -H 'accept: application/json' \
 -H 'Content-Type: application/json' \
 -d '{
      "model": "embedding-model",
      "input": "Hello world",
      "user": "test"
}'

curl -k -X 'POST' '/embeddings' \
 -H 'accept: application/json' \
 -H 'Content-Type: application/json' \
 -d '{
      "model": "embedding-model",
      "input": "Hello world",
      "user": ""
}'

curl -k -X 'POST' '/embeddings' \
 -H 'accept: application/json' \
 -H 'Content-Type: application/json' \
 -d '{
      "model": "embedding-model",
      "input": "Hello world"
}'
```

sashabaranov: @nagar-ajay thank you for the PR! Please the latest changes from `master` here, so the CI will pass
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/sashabaranov/go-openai/pull/899?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 98.86%. Comparing base [(`774fc9d`)](https://app.codecov.io/gh/sashabaranov/go-openai/commit/774fc9dd12ed60c10a9f9f03319ddb9cd5f8780c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov) to head [(`df90855`)](https://app.codecov.io/gh/sashabaranov/go-openai/commit/df9085509ce7bf37f292d89b61ae21ff231a9b9f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov).
> Report is 71 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #899      +/-   ##
==========================================
+ Coverage   98.46%   98.86%   +0.40%     
==========================================
  Files          24       26       +2     
  Lines        1364     1761     +397     
==========================================
+ Hits         1343     1741     +398     
+ Misses         15       14       -1     
  Partials        6        6              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/sashabaranov/go-openai/pull/899?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Baranov).

----
🚨 Try these New Features:

- [Flaky Tests Detection](https://docs.codecov.com/docs/test-result-ingestion-beta) - Detect and resolve failed and flaky tests
nagar-ajay: Updated the PR.

Patch:
diff --git a/completion.go b/completion.go
index 77ea8c3..9e30736 100644
--- a/completion.go
+++ b/completion.go
@@ -15,7 +15,6 @@ var (
 
 var (
 	ErrO1BetaLimitationsMessageTypes = errors.New("this model has beta-limitations, user and assistant messages only, system messages are not supported")                                  //nolint:lll
-	ErrO1BetaLimitationsStreaming    = errors.New("this model has beta-limitations, streaming not supported")                                                                              //nolint:lll
 	ErrO1BetaLimitationsTools        = errors.New("this model has beta-limitations, tools, function calling, and response format parameters are not supported")                            //nolint:lll
 	ErrO1BetaLimitationsLogprobs     = errors.New("this model has beta-limitations, logprobs not supported")                                                                               //nolint:lll
 	ErrO1BetaLimitationsOther        = errors.New("this model has beta-limitations, temperature, top_p and n are fixed at 1, while presence_penalty and frequency_penalty are fixed at 0") //nolint:lll
@@ -199,12 +198,6 @@ func validateRequestForO1Models(request ChatCompletionRequest) error {
 		return ErrO1MaxTokensDeprecated
 	}
 
-	// Beta Limitations
-	// refs:https://platform.openai.com/docs/guides/reasoning/beta-limitations
-	// Streaming: not supported
-	if request.Stream {
-		return ErrO1BetaLimitationsStreaming
-	}
 	// Logprobs: not supported.
 	if request.LogProbs {
 		return ErrO1BetaLimitationsLogprobs
diff --git a/embeddings.go b/embeddings.go
index 74eb8aa..4a0e682 100644
--- a/embeddings.go
+++ b/embeddings.go
@@ -155,7 +155,7 @@ const (
 type EmbeddingRequest struct {
 	Input          any                     `json:"input"`
 	Model          EmbeddingModel          `json:"model"`
-	User           string                  `json:"user"`
+	User           string                  `json:"user,omitempty"`
 	EncodingFormat EmbeddingEncodingFormat `json:"encoding_format,omitempty"`
 	// Dimensions The number of dimensions the resulting output embeddings should have.
 	// Only supported in text-embedding-3 and later models.


Test patch:
diff --git a/batch_test.go b/batch_test.go
index 4b2261e..f4714f4 100644
--- a/batch_test.go
+++ b/batch_test.go
@@ -211,7 +211,7 @@ func TestUploadBatchFileRequest_AddEmbedding(t *testing.T) {
 					Input: []string{"Hello", "World"},
 				},
 			},
-		}, []byte("{\"custom_id\":\"req-1\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"gpt-3.5-turbo\",\"user\":\"\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}\n{\"custom_id\":\"req-2\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"text-embedding-ada-002\",\"user\":\"\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}")}, //nolint:lll
+		}, []byte("{\"custom_id\":\"req-1\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"gpt-3.5-turbo\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}\n{\"custom_id\":\"req-2\",\"body\":{\"input\":[\"Hello\",\"World\"],\"model\":\"text-embedding-ada-002\"},\"method\":\"POST\",\"url\":\"/v1/embeddings\"}")}, //nolint:lll
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
diff --git a/chat_stream_test.go b/chat_stream_test.go
index 1468414..28a9acf 100644
--- a/chat_stream_test.go
+++ b/chat_stream_test.go
@@ -36,27 +36,6 @@ func TestChatCompletionsStreamWrongModel(t *testing.T) {
 	}
 }
 
-func TestChatCompletionsStreamWithO1BetaLimitations(t *testing.T) {
-	config := openai.DefaultConfig("whatever")
-	config.BaseURL = "http://localhost/v1/chat/completions"
-	client := openai.NewClientWithConfig(config)
-	ctx := context.Background()
-
-	req := openai.ChatCompletionRequest{
-		Model: openai.O1Preview,
-		Messages: []openai.ChatCompletionMessage{
-			{
-				Role:    openai.ChatMessageRoleUser,
-				Content: "Hello!",
-			},
-		},
-	}
-	_, err := client.CreateChatCompletionStream(ctx, req)
-	if !errors.Is(err, openai.ErrO1BetaLimitationsStreaming) {
-		t.Fatalf("CreateChatCompletion should return ErrO1BetaLimitationsStreaming, but returned: %v", err)
-	}
-}
-
 func TestCreateChatCompletionStream(t *testing.T) {
 	client, server, teardown := setupOpenAITestServer()
 	defer teardown()