Skip to content

refactor(core): add LiteLLMControlParams and thread litellm_params into custom stream wrappers - #42559

Draft
devin-ai-integration[bot] wants to merge 1 commit into
litellm_leak_1_fencefrom
litellm_leak_2_control_bag
Draft

devin-ai-integration[bot] wants to merge 1 commit into
litellm_leak_1_fencefrom
litellm_leak_2_control_bag

Conversation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • stream_chunk_size has no typed home, it rides as a loose top-level kwarg
  • custom stream wrappers never receive litellm_params, so no provider can read control values
  • PR 3 (making Bedrock Invoke honor stream_chunk_size) needs both before it can be small

How it solves it:

  • adds frozen slotted LiteLLMControlParams(stream_chunk_size: int | None) with from_kwargs
  • all_litellm_params derives the control field names via dataclasses.fields, plus the control key
  • get_litellm_params emits litellm_params["control"], built first thing in completion()
  • get_sync_custom_stream_wrapper / get_async_custom_stream_wrapper take keyword-only litellm_params (base and all seven overrides)
  • both llm_http_handler call sites pass litellm_params
  • nothing reads control yet

This PR is plumbing with no intended behavior change. It is split out so PR 3 is a small behavioral diff on top of a signature change that touches 12 source files. Stacked on #42529 (litellm_leak_1_fence), which fences internal params off provider bodies

User Flow

Before: a developer streaming Bedrock Invoke through the proxy asks for 64 byte chunks and gets the transport default anyway

  1. They send POST https://litellm-domain/v1/chat/completions with "model": "bedrock/anthropic.claude-... (invoke path), "stream": true and "stream_chunk_size": 64
  2. SSE chunks arrive in the upstream's default sizing, the 64 is silently ignored
  3. The provider request body does not contain stream_chunk_size (fenced by PR 1)

After: identical, this PR changes nothing the user can observe

  1. They send the same POST https://litellm-domain/v1/chat/completions with "stream": true and "stream_chunk_size": 64
  2. SSE chunks still arrive in the upstream's default sizing (PR 3 makes Invoke honor it)
  3. The provider request body still does not contain stream_chunk_size

The only user-visible difference is the edge case in Caveats: a caller passing a provider-specific kwarg literally named control had it forwarded before and has it dropped now

Relevant issues

Affected release

Linear ticket

Part of LIT-8316

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

This PR claims no observable difference, so the proof is a base vs head A/B showing the provider wire is byte-for-byte the same. Shared setup: the scripted upstream from tests/integration/_support/upstream.py (returns 400 when an internal field appears in a body), litellm.disable_aiohttp_transport = True, LITELLM_RUST=false, httpx.Client.send / AsyncClient.send recorded for status, httpx.Response.iter_bytes / aiter_bytes recorded for chunk_size. 24 cases: openai, azure, anthropic, gemini, bedrock_converse, bedrock_invoke, each stream and non-stream, each through litellm.completion and litellm.acompletion, all with stream_chunk_size=64. Provider APIs are not hit because the point is the request body sent to them, not the answer

Before (d6a4fb9, merge base litellm_leak_1_fence)

  1. python probe_wire.py > probe_base.json from a worktree at the merge base (litellm.__file__ confirmed pointing at the worktree)
  2. All 24 cases: text fenced, exactly 1 upstream request, status [200], no denylisted key in any body at any depth
  3. iter_bytes / aiter_bytes chunk_size is 64 for bedrock_converse-stream-sync and bedrock_converse-stream-async, None everywhere else (Bedrock Invoke drops it)

After (0badb16)

  1. python probe_wire.py > probe_head.json from this branch
  2. All 24 cases: text fenced, exactly 1 upstream request, status [200], no denylisted key in any body at any depth
  3. iter_bytes / aiter_bytes chunk_size is 64 for the two bedrock_converse stream cases, None everywhere else
  4. diff probe_base.json probe_head.json prints nothing: identical text, request count, targets, status, sorted body key set at every depth, and chunk sizes

Revert-and-red for the new tests: with litellm/ restored to the merge base and the test files kept, pytest tests/test_litellm/types/test_types_utils.py tests/test_litellm/litellm_core_utils/test_get_litellm_params.py tests/test_litellm/test_main.py -k control gives 4 failed; restored, 4 passed. Deleting control=control, in completion() alone fails test_completion_puts_control_in_litellm_params_seen_by_pre_call_hook with assert None == LiteLLMControlParams(stream_chunk_size=64)

make check passes (ruff strict, test-quality, type-discipline, basedpyright ratchets, format, OpenAPI/dashboard sync)

Type

🧹 Refactoring

Caveats (if any)

Low

  • literal control in all_litellm_params is a small behavior change
    • a caller passing a provider-specific kwarg named control had it forwarded (openai: extra_body) before, it is dropped now
    • also excluded from the cache key
    • no in-repo provider consumes a control request param, so blast radius is negligible
    • PR 3 may rename the key (e.g. litellm_control) if this matters
  • litellm_params is a required keyword-only argument on the wrapper hooks
    • an out-of-tree BaseConfig subclass overriding them with the old signature raises TypeError at call time
    • zero such overrides in-repo, enterprise/ included
  • stream_chunk_size now reaches litellm_params twice: raw top-level key (kept for Bedrock Converse) and typed inside control
    • both derive from the same kwargs entry; they only disagree on non-int input ("64"), where control is None and the raw key keeps the caller value
    • nothing reads control yet; PR 3 should collapse the raw key onto control and decide whether to coerce or reject non-int
  • new test lines carry LIT010 / LIT002 debits absorbed by the budget
    • control_field_names without Final and mutable set / dict literals used as test recorders
    • fix is frozenset(...) and an immutable recorder; left for a follow-up so this PR stays a pure plumbing diff
  • basedpyright rises by 2 on llm_http_handler.py at the two new keyword arguments (litellm_params is an untyped dict there), within the ratchet
  • 3 of the 4 new tests go red on revert only through ImportError; the test_main one is the behavioral guard
  • deviations from the spec: Mapping[str, object] instead of dict (AGENTS.md bans bare dict and it pushed basedpyright past budget; dict is assignable so callers are unchanged), and plain annotations instead of Final on the frozen dataclass fields, matching _CompletionDispatchContext in litellm/types/completion.py

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/fa7328741dc445458025597e8fc3ea74
Open in Devin Desktop: https://app.devin.ai/desktop/session/fa7328741dc445458025597e8fc3ea74?variant=devin
Requested by: @shreshth-berri

…to custom stream wrappers

Builds a frozen LiteLLMControlParams once at the top of completion() from kwargs and carries it as
litellm_params["control"] through get_litellm_params. all_litellm_params now derives its control entries from the
dataclass fields, replacing the literal stream_chunk_size entry, and also denylists the control key itself.
get_sync_custom_stream_wrapper and get_async_custom_stream_wrapper take a keyword-only litellm_params argument in the
base config and all seven provider overrides, passed from both llm_http_handler call sites. Nothing reads control yet
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@codecov

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/llms/langgraph/chat/transformation.py 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@CLAassistant

CLAassistant commented Oct 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
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.

2 participants