Skip to content

Conversation

@AyushSawant18588
Copy link
Contributor

@AyushSawant18588 AyushSawant18588 commented Nov 23, 2024

Describe the change
This change adds support for an ExtraBody parameter in the EmbeddingRequest struct, allowing developers to include additional, arbitrary key-value pairs in the API request payload. This ensures extensibility and flexibility when working with the OpenAI Embeddings API, especially for experimental features, future API updates, or custom parameters that are not explicitly defined in the current request structure. The addition of ExtraBody is particularly valuable for supporting LLM engines like vLLM, Nvidia NIM, etc, which may require additional fields beyond the standard parameters. Refrence
There is an issue created that ExtraBody parameter support is required for vLLM engine also. #898
In this PR ExtraBody parameter support is added only for embedding API but it can be extended for other APIs as well like chat completions

Provide OpenAI documentation link
OpenAI's official python client also supports this extra_body field which this go client could also support. Refrence

Describe your solution
The solution involves:
Introducing the ExtraBody field in the EmbeddingRequest struct. This is a map of string keys to arbitrary values (map[string]any) that allows developers to pass additional fields to the API dynamically.
Updating the CreateEmbeddings method to merge the ExtraBody values into the main request payload (body) before sending it to the API. This is done through the withExtraBody request option, which ensures the fields in ExtraBody are included without overwriting existing mandatory parameters.

Tests
Added unit tests and done sanity check of embeddings api with extra_body param for Nvidia NIM embedding model

@codecov
Copy link

codecov bot commented Nov 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.71%. Comparing base (774fc9d) to head (bc7a940).
Report is 122 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #906      +/-   ##
==========================================
+ Coverage   98.46%   98.71%   +0.25%     
==========================================
  Files          24       26       +2     
  Lines        1364     1789     +425     
==========================================
+ Hits         1343     1766     +423     
- Misses         15       16       +1     
- Partials        6        7       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AyushSawant18588
Copy link
Contributor Author

@sashabaranov Can you please look into this?

@AyushSawant18588
Copy link
Contributor Author

@sashabaranov Can you please look into this PR? thanks

@gavrissh
Copy link

gavrissh commented Jan 2, 2025

@sashabaranov Would require your assistance in getting these changes in. Thank you

@nagar-ajay
Copy link
Contributor

@sashabaranov can you please check this? Thanks!

@sashabaranov
Copy link
Owner

@AyushSawant18588 Thank you for updating the PR! I'm re-reading it for the fifth time, and there's something odd about it—and I can't pinpoint what it is exactly. It feels like we're adding a lot of counterintuitive logic for a tiny use case.

If the goal is to add {"input_type": "query", "truncate": "NONE"} to Embedding requests, maybe that's literally what we should do — just add two fields to EmbeddingRequest. Or are there more use cases I don't know of?

@AyushSawant18588
Copy link
Contributor Author

AyushSawant18588 commented Jan 16, 2025

@sashabaranov So the official OpenAI Python client offers an extra_body field for all inference APIs. For example, in the embedding API implementation, you can see this feature being used here: Embeddings API. Their implementation and behaviour is same as done in this PR.
The extra_body parameter allows users to add arbitrary JSON properties to the request body. This approach is particularly useful when working with diverse LLM engines that may introduce additional or custom fields in their APIs. Instead of updating the struct for every unique field, this mechanism provides flexibility to include extra fields as needed without modifying the core structure repeatedly.
Here are some examples where extra parameters may vary depending on the API and LLM engine:

There is an issue created that ExtraBody parameter support is required for Completions API as well for vLLM engine. #898
This same structure can be used there as well.

@johnugeorge
Copy link

@sashabaranov

  1. We see that all embedding servers including vLLM, Nvidia NIM support "extra_body" in the embedding request.
  2. ExtraBody is supported in official OpenAI Python clients.
  3. "input_type": "query", "truncate": "NONE" cannot be added to the embedding request as it will break OpenAI compatible server implementations(it is not compatible with OpenAI API standard)

Can you please look into this?

@lilien1010
Copy link

@sashabaranov Help!

@justa-cai
Copy link
Contributor

The Qwen3 LLM now supports switching between reasoning and non-reasoning modes, which can be controlled using this parameter. Would it be possible to merge this change?

@SteveHere SteveHere mentioned this pull request Jun 30, 2025
Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sashabaranov sashabaranov merged commit bd36c45 into sashabaranov:master Jul 11, 2025
3 checks passed
@sashabaranov sashabaranov requested a review from Copilot July 11, 2025 16:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR extends the Embeddings API client to allow arbitrary key-value parameters via a new ExtraBody field, enabling greater flexibility for nonstandard or experimental embedding engines.

  • Introduce ExtraBody in EmbeddingRequest, EmbeddingRequestStrings, and EmbeddingRequestTokens.
  • Update CreateEmbeddings to merge ExtraBody entries into the JSON request payload.
  • Add withExtraBody request option and corresponding unit tests.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
embeddings.go Added ExtraBody field and merged it into the request payload.
client.go Implemented withExtraBody to inject extra parameters.
embeddings_test.go Added tests for marshaling and CreateEmbeddings with ExtraBody.
Comments suppressed due to low confidence (2)

embeddings_test.go:68

  • This test only verifies the model field. You should also assert that the marshaled JSON includes the ExtraBody keys (e.g., "input_type" and "truncate") to ensure ExtraBody is serialized correctly.
		if !bytes.Contains(marshaled, []byte(`"model":"`+model+`"`)) {

embeddings_test.go:161

  • [nitpick] This comment is incomplete and unclear. Consider rewriting it to describe the test intent, for example: // test CreateEmbeddings returns an error for requests with non-serializable fields.
	// test create embeddings with strings (ExtraBody in request and )


// Deserialize JSON to map[string]any
var body map[string]any
_ = json.Unmarshal(jsonData, &body)
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid ignoring the error returned by json.Unmarshal. Capture and handle the error (e.g., if err := json.Unmarshal(...); err != nil { return res, err }) to prevent silent failures.

Suggested change
_ = json.Unmarshal(jsonData, &body)
if err := json.Unmarshal(jsonData, &body); err != nil {
return res, err
}

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AyushSawant18588 just some tech debt :)

Rosenberg96 pushed a commit to lunarway/go-openai that referenced this pull request Aug 18, 2025
* support for extra_body parameter for embeddings API

* done linting

* added unit tests

* improved code coverage and removed unnecessary checks

* test cleanup

* updated body map creation code

* code coverage

* minor change

* updated testcase comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants