@@ -322,6 +322,9 @@ type ChatCompletionRequest struct {
322
322
ServiceTier ServiceTier `json:"service_tier,omitempty"`
323
323
// Embedded struct for non-OpenAI extensions
324
324
ChatCompletionRequestExtensions
325
+ // The ExtraBody field allows for the inclusion of arbitrary key-value pairs
326
+ // in the request body that may not be explicitly defined in this struct.
327
+ ExtraBody map [string ]any `json:"extra_body,omitempty"`
325
328
}
326
329
327
330
type StreamOptions struct {
@@ -472,11 +475,28 @@ func (c *Client) CreateChatCompletion(
472
475
return
473
476
}
474
477
478
+ // The body map is used to dynamically construct the request payload for the embedding API.
479
+ // Instead of relying on a fixed struct, the body map allows for flexible inclusion of fields
480
+ // based on their presence, avoiding unnecessary or empty fields in the request.
481
+ extraBody := request .ExtraBody
482
+ request .ExtraBody = nil
483
+
484
+ // Serialize baseReq to JSON
485
+ jsonData , err := json .Marshal (request )
486
+ if err != nil {
487
+ return
488
+ }
489
+
490
+ // Deserialize JSON to map[string]any
491
+ var body map [string ]any
492
+ _ = json .Unmarshal (jsonData , & body )
493
+
475
494
req , err := c .newRequest (
476
495
ctx ,
477
496
http .MethodPost ,
478
497
c .fullURL (urlSuffix , withModel (request .Model )),
479
- withBody (request ),
498
+ withBody (body ), // Main request body.
499
+ withExtraBody (extraBody ), // Merge ExtraBody fields.
480
500
)
481
501
if err != nil {
482
502
return
0 commit comments