Skip to content

fix: Add extra_body to OpenAI client chat request#1453

Open
majiayu000 wants to merge 2 commits intotmc:mainfrom
majiayu000:fix-1436-add-extrabody-to-openai-client-1230-1643
Open

fix: Add extra_body to OpenAI client chat request#1453
majiayu000 wants to merge 2 commits intotmc:mainfrom
majiayu000:fix-1436-add-extrabody-to-openai-client-1230-1643

Conversation

@majiayu000
Copy link
Copy Markdown
Contributor

Summary

This PR fixes #1436 by adding support for the extra_body parameter to the OpenAI client, similar to the official Python client.

Changes

  • Added ExtraBody field to ChatRequest struct for passing custom parameters
  • Custom JSON marshaling that merges ExtraBody fields into the request body
  • WithExtraBody() call option for users to pass custom parameters via metadata
  • Comprehensive tests for marshaling behavior, including edge cases (nil, empty, nested objects, field overrides)

Use Case

This allows users to pass beta features or new OpenAI parameters (e.g., parallel_tool_calls) without requiring library updates:

llm.GenerateContent(ctx, messages,
    openai.WithExtraBody(map[string]interface{}{
        "parallel_tool_calls": false,
    }),
)

Testing

  • All existing tests pass
  • Added unit tests for WithExtraBody option
  • Added comprehensive marshaling tests for ExtraBody field

Generated with Claude Code

Add support for extra_body parameter to allow passing custom
parameters to the OpenAI API. This enables users to use beta
features or new parameters without requiring library updates.

- Add ExtraBody field to ChatRequest struct
- Update MarshalJSON to merge ExtraBody fields into request
- Add WithExtraBody call option
- Add comprehensive unit tests

Fixes tmc#1436

Signed-off-by: majiayu000 <1835304752@qq.com>
len() for nil maps returns 0, so the nil check is redundant.
This fixes the staticcheck S1009 lint error.
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.

Add extra_body to OpenAI client chat request

1 participant