-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support extra_body parameters #1069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 6 commits
9626571
e373ca0
c18b4e2
2112a9d
ed7f01d
095ee5f
a727da4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -325,6 +325,11 @@ type ChatCompletionRequest struct { | |||||||||||
| // We recommend hashing their username or email address, in order to avoid sending us any identifying information. | ||||||||||||
| // https://platform.openai.com/docs/api-reference/chat/create#chat_create-safety_identifier | ||||||||||||
| SafetyIdentifier string `json:"safety_identifier,omitempty"` | ||||||||||||
| // ExtraBody provides configuration options for the generation process in Gemini API. | ||||||||||||
| // Additional configuration parameters to control model behavior. Will be passed directly to the Gemini API. | ||||||||||||
| // Such as thinking mode for Gemini. "extra_body": {"google": {"thinking_config": {"include_thoughts": true}}} | ||||||||||||
| // https://ai.google.dev/gemini-api/docs/openai | ||||||||||||
| ExtraBody map[string]any `json:"extra_body,omitempty"` | ||||||||||||
| // Embedded struct for non-OpenAI extensions | ||||||||||||
| ChatCompletionRequestExtensions | ||||||||||||
| } | ||||||||||||
|
|
@@ -477,11 +482,28 @@ func (c *Client) CreateChatCompletion( | |||||||||||
| return | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // The body map is used to dynamically construct the request payload for the chat completion API. | ||||||||||||
| // Instead of relying on a fixed struct, the body map allows for flexible inclusion of fields | ||||||||||||
| // based on their presence, avoiding unnecessary or empty fields in the request. | ||||||||||||
| extraBody := request.ExtraBody | ||||||||||||
| request.ExtraBody = nil | ||||||||||||
|
|
||||||||||||
| // Serialize request to JSON | ||||||||||||
| jsonData, err := json.Marshal(request) | ||||||||||||
| if err != nil { | ||||||||||||
| return | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Deserialize JSON to map[string]any | ||||||||||||
| var body map[string]any | ||||||||||||
| _ = json.Unmarshal(jsonData, &body) | ||||||||||||
|
||||||||||||
| _ = json.Unmarshal(jsonData, &body) | |
| err = json.Unmarshal(jsonData, &body) | |
| if err != nil { | |
| return | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you are using this correctly: withExtraBody merges extraBody into request body. However, in the example you've mentioned it goes as a separate field:
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-2.5-flash",
"messages": [{"role": "user", "content": "Explain to me how AI works"}],
"extra_body": {
"google": {
"thinking_config": {
"include_thoughts": true
}
}
}
}'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your inspection. I have seen it working correctly during testing and use. For example:
_, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
Model: "doubao-seed-1-6-250615",
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Hello!",
},
},
ExtraBody: map[string]any{
"thinking":map[string]any {
"type": "disabled",
},
},
})
After the request is processed as the body, it will change to:
{
"model": "doubao-seed-1-6-250615",
"messages": [{"role": "user", "content": "Hello!"}],
"thinking": {
"type": "disabled"
}
}
For the gemini example, I'm sorry that I didn't give a clear example that caused your misunderstanding. In Gemini's official API, they have additional support for extra_body fields. Let me explain the example call in the documentation:
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
model="gemini-2.5-flash",
messages=[{"role": "user", "content": "Explain to me how AI works"}],
extra_body={
'extra_body': {
"google": {
"thinking_config": {
"thinking_budget": 800,
"include_thoughts": True
}
}
}
}
)
They use OpenAI's official python library OpenAI. In OpenAI extra_body will be promoted to root. The final body is:
{
"messages": [
{
"role": "user",
"content": "Explain to me how AI works"
}
],
"model": "gemini-2.5-flash",
"extra_body": {
"google": {
"thinking_config": {
"thinking_budget": 800,
"include_thoughts": true
}
}
}
}
So Gemini's example is actually to ensure that extra_body is still under extra_body after being extracted to root, so there is an extra layer of extra_body.
Like the example in #1025, the requirement is to extract the contents of extra_body to the root.
I put a sample code here, which can print out the processed body, you can test it:
import httpx
from openai import OpenAI
def log_request(request):
print("\n===== HTTP Request Details ======")
print(f"Method: {request.method}")
print(f"URL: {request.url}")
print("Headers:")
for key, value in request.headers.items():
print(f" {key}: {value}")
print("Body:")
print(request.content.decode())
print("================================\n")
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
http_client=httpx.Client(event_hooks={"request": [log_request]}),
)
response = client.chat.completions.create(
model="gemini-2.5-flash",
messages=[{"role": "user", "content": "Explain to me how AI works"}],
extra_body={
'extra_body': {
"google": {
"thinking_config": {
"thinking_budget": 800,
"include_thoughts": True
}
}
}
}
)
print(response)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment describes general benefits of using a map but doesn't explain the specific purpose of this code block, which is to handle ExtraBody field merging. Consider updating to: 'Create a dynamic request body by merging ExtraBody fields into the main request payload.'