mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-09 03:18:44 +00:00
refactor(completion): extract provider dispatch into typed helpers so basedpyright can analyze it
completion packed a ~2,900-line per-provider if/elif dispatch into a single body, pushing it past basedpyright's code-flow complexity ceiling. basedpyright emitted "Code is too complex to analyze" and skipped the whole function, so every type error on the hottest request path was invisible and unguarded. This extracts each provider branch into its own helper that receives a single frozen _CompletionDispatchContext carrying the shared locals the dispatch reads (model, messages, optional_params, litellm_params, logging, headers, api_key, api_base, client, timeout, ...). Each helper destructures only what it uses and keeps its body verbatim, so the dispatch logic is unchanged; completion now sits well under the ceiling and basedpyright type-checks it and all 61 helpers. The context is built once after setup, right before the dispatch, and each arm becomes `response = _complete_<provider>(ctx)` feeding the existing single `return response`, preserving the original "dispatch sets response, return once" shape and the early-return/streaming semantics. The three deprecated no-op arms (clarifai, together_ai, palm) stay inline. Building the context surfaced a shadowing trap: acompletion, client, api_version, organization and text_completion are completion parameters that shadow module-level names, so a naive free-variable pass would have dropped them from the context and silently changed behavior; they are threaded through explicitly. Restoring analysis also surfaced get_secret()'s bool-inclusive return type broadening api_base in the anthropic and anthropic_text branches (it flows into .endswith()/+=); api_base is narrowed back to Optional[str] at those two sites. Genuinely dead assignments the monolith hid at function scope (an unused prompt build, two unused data dicts, and two no-op custom_llm_provider reassignments) are removed. No behavior change. The basedpyright per-rule budget is unchanged; the gate is confirmed by the CI run (the dev-only/local basedpyright env inflates reportMissingTypeStubs on any main.py edit, independent of this change, as the budget's own git history documents).
This commit is contained in:
parent
7e5699c7ab
commit
1c2041177e
1 changed files with 4019 additions and 2714 deletions
6733
litellm/main.py
6733
litellm/main.py
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue