Skip to content

fix(models): merge http_options.extra_body into the constructor's extra_body in LiteLlm - #8506

Open
RobertWei3 wants to merge 1 commit into
google:mainfrom
RobertWei3:fix/litellm-merge-extra-body
Open

RobertWei3 wants to merge 1 commit into
google:mainfrom
RobertWei3:fix/litellm-merge-extra-body

Conversation

@RobertWei3

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
When a LiteLlm model is created with extra_body=... and a request also sets http_options.extra_body (directly, through RunConfig(http_options=...), or in a before_model_callback), the request's dict replaces the constructor's dict. Constructor fields such as LiteLLM proxy metadata / tags, Qwen enable_thinking, vLLM chat_template_kwargs or DeepSeek thinking are silently dropped for that request.

Solution:
Merge the request-level extra_body into the constructor's extra_body, the same way http_opts.headers is merged into extra_headers a few lines above:

  • Keys set on the request win on conflict.
  • A new dict is built each time, so the dict passed to the constructor is not modified between requests.
  • The merge is shallow, consistent with how headers are handled.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added test_generate_content_async_merges_http_options_extra_body_with_constructor in tests/unittests/models/test_litellm.py. It checks that constructor keys are kept, request keys win on conflict, and the constructor's dict is not mutated. The test fails on main and passes with this change.

$ pytest tests/unittests/models/test_litellm.py -q -k extra_body
2 passed, 899 deselected
 
$ pytest tests/unittests/models/ -q
2245 passed, 33 warnings

Manual End-to-End (E2E) Tests:

Ran the reproduction script from #8505, which sends a real request through LiteLlm -> litellm -> a local fake OpenAI-compatible server and prints the request body the server received. The constructor sets extra_body={"metadata": {"team": "a"}} and the request sets http_options.extra_body={"priority": "high"}.

Before (main):

metadata sent: <missing>
priority sent: high

After (this PR):

metadata sent: {'team': 'a'}
priority sent: high

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

PR #8331 changes the num_retries lines just above this block. The two changes do not overlap, and I checked that this patch still applies cleanly on top of #8331.

pyink and isort pass on both changed files.

…ra_body in LiteLlm

A per-request http_options.extra_body used to replace the extra_body passed to the LiteLlm constructor, silently dropping fields such as LiteLLM proxy metadata. Merge the two the same way extra_headers is merged: request keys win, and the constructor's dict is not mutated.
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.

LiteLlm: http_options.extra_body replaces the constructor's extra_body instead of merging with it

1 participant