Repository navigation
refactor(core): add LiteLLMControlParams and thread litellm_params into custom stream wrappers - #42559
Draft
devin-ai-integration[bot] wants to merge 1 commit into
Draft
refactor(core): add LiteLLMControlParams and thread litellm_params into custom stream wrappers#42559devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
…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
Contributor
Author
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TLDR
Problem this solves:
stream_chunk_sizehas no typed home, it rides as a loose top-level kwarglitellm_params, so no provider can read control valuesstream_chunk_size) needs both before it can be smallHow it solves it:
LiteLLMControlParams(stream_chunk_size: int | None)withfrom_kwargsall_litellm_paramsderives the control field names viadataclasses.fields, plus thecontrolkeyget_litellm_paramsemitslitellm_params["control"], built first thing incompletion()get_sync_custom_stream_wrapper/get_async_custom_stream_wrappertake keyword-onlylitellm_params(base and all seven overrides)llm_http_handlercall sites passlitellm_paramscontrolyetThis 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 bodiesUser Flow
Before: a developer streaming Bedrock Invoke through the proxy asks for 64 byte chunks and gets the transport default anyway
"model": "bedrock/anthropic.claude-...(invoke path),"stream": trueand"stream_chunk_size": 64stream_chunk_size(fenced by PR 1)After: identical, this PR changes nothing the user can observe
"stream": trueand"stream_chunk_size": 64stream_chunk_sizeThe only user-visible difference is the edge case in Caveats: a caller passing a provider-specific kwarg literally named
controlhad it forwarded before and has it dropped nowRelevant 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
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@greptileaito 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.sendrecorded for status,httpx.Response.iter_bytes/aiter_bytesrecorded forchunk_size. 24 cases: openai, azure, anthropic, gemini, bedrock_converse, bedrock_invoke, each stream and non-stream, each throughlitellm.completionandlitellm.acompletion, all withstream_chunk_size=64. Provider APIs are not hit because the point is the request body sent to them, not the answerBefore (d6a4fb9, merge base litellm_leak_1_fence)
python probe_wire.py > probe_base.jsonfrom a worktree at the merge base (litellm.__file__confirmed pointing at the worktree)fenced, exactly 1 upstream request, status[200], no denylisted key in any body at any depthiter_bytes/aiter_byteschunk_sizeis 64 forbedrock_converse-stream-syncandbedrock_converse-stream-async,Noneeverywhere else (Bedrock Invoke drops it)After (0badb16)
python probe_wire.py > probe_head.jsonfrom this branchfenced, exactly 1 upstream request, status[200], no denylisted key in any body at any depthiter_bytes/aiter_byteschunk_sizeis 64 for the twobedrock_conversestream cases,Noneeverywhere elsediff probe_base.json probe_head.jsonprints nothing: identical text, request count, targets, status, sorted body key set at every depth, and chunk sizesRevert-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 controlgives 4 failed; restored, 4 passed. Deletingcontrol=control,incompletion()alone failstest_completion_puts_control_in_litellm_params_seen_by_pre_call_hookwithassert None == LiteLLMControlParams(stream_chunk_size=64)make checkpasses (ruff strict, test-quality, type-discipline, basedpyright ratchets, format, OpenAPI/dashboard sync)Type
🧹 Refactoring
Caveats (if any)
Low
controlinall_litellm_paramsis a small behavior changecontrolhad it forwarded (openai:extra_body) before, it is dropped nowcontrolrequest param, so blast radius is negligiblelitellm_control) if this matterslitellm_paramsis a required keyword-only argument on the wrapper hooksBaseConfigsubclass overriding them with the old signature raisesTypeErrorat call timeenterprise/includedstream_chunk_sizenow reacheslitellm_paramstwice: raw top-level key (kept for Bedrock Converse) and typed insidecontrol"64"), wherecontrolisNoneand the raw key keeps the caller valuecontrolyet; PR 3 should collapse the raw key ontocontroland decide whether to coerce or reject non-intcontrol_field_nameswithoutFinaland mutable set / dict literals used as test recordersfrozenset(...)and an immutable recorder; left for a follow-up so this PR stays a pure plumbing diffllm_http_handler.pyat the two new keyword arguments (litellm_paramsis an untypeddictthere), within the ratchetImportError; thetest_mainone is the behavioral guardMapping[str, object]instead ofdict(AGENTS.md bans baredictand it pushed basedpyright past budget;dictis assignable so callers are unchanged), and plain annotations instead ofFinalon the frozen dataclass fields, matching_CompletionDispatchContextinlitellm/types/completion.pyFinal Attestation
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