Skip to content

fix(openai): repair the annotation that makes litellm unimportable - #35702

Closed
yassin-berriai wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_fix_openai_common_utils_union
Closed

fix(openai): repair the annotation that makes litellm unimportable#35702
yassin-berriai wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_fix_openai_common_utils_union

Conversation

@yassin-berriai

@yassin-berriai yassin-berriai commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • import litellm raises NameError: name 'Union' is not defined
  • litellm_internal_staging is red on every open PR
  • ruff check reports F821 at llms/openai/common_utils.py:138

How it solves it:

  • Rewrites the annotation as a PEP 604 union

Relevant issues

Linear ticket

Pre-Submission checklist

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

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • 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

Both runs are a plain import litellm against a worktree at the named commit, nothing mocked

$ git log --oneline -1
9d5984b358 refactor(ui): rename the create MCP server component to PascalCase (#35686)

$ python -c "import litellm; print('import ok')"
  File ".../litellm/llms/openai/common_utils.py", line 138, in BaseOpenAILLM
    def owns_wrapped_http_client(http_client: Optional[Union[httpx.Client, httpx.AsyncClient]]) -> bool:
                                                       ^^^^^
NameError: name 'Union' is not defined
$ git log --oneline -1
<this PR's head>

$ python -c "import litellm.llms.openai.common_utils as m; import httpx; print(m.BaseOpenAILLM.owns_wrapped_http_client(None), m.BaseOpenAILLM.owns_wrapped_http_client(httpx.Client()))"
True True

$ cd litellm && ruff check .
All checks passed!

Before the fix, cd litellm && ruff check . (the lint job's exact command) reports:

F821 Undefined name `Union`
   --> llms/openai/common_utils.py:138:56
Found 1 error.

Type

🐛 Bug Fix

Changes

BaseOpenAILLM.owns_wrapped_http_client annotates its parameter with Optional[Union[httpx.Client, httpx.AsyncClient]], but Union was never added to the module's typing import. The annotation is evaluated when the class body executes, so the NameError fires at import rather than at call time, and every module that imports litellm dies with it.

The annotation becomes httpx.Client | httpx.AsyncClient | None rather than gaining a Union import. requires-python is >=3.10, so PEP 604 unions evaluate at runtime, and both UP007 and UP045 sit at a ceiling of 0 in ruff-strict-budget.json, so adding the import would have traded the F821 for a budget failure. That is not hypothetical: the first push of this branch did exactly that and the lint job went red on UP007: total 67 over limit 0 (this change added 1).

That is why lint and the entire test matrix are failing on open PRs whose diffs contain no Python: CI evaluates the PR merged into the current staging tip, so staging's breakage is attributed to each PR.

No test is added. The regression is "the package imports at all", which every existing test in the suite already asserts as a precondition; the whole matrix going from red to green is the assertion.

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

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

`BaseOpenAILLM.owns_wrapped_http_client` annotates its parameter with
`Optional[Union[httpx.Client, httpx.AsyncClient]]`, but `Union` was never added
to the module's typing import. The annotation is evaluated when the class body
executes, so `import litellm` raises `NameError: name 'Union' is not defined`
and every module that imports litellm dies with it.

litellm_internal_staging is red from this: `cd litellm && ruff check .` reports
F821 and the whole test matrix fails on import, on every open PR, because CI
evaluates the PR merged into the current staging tip.

The annotation becomes `httpx.Client | httpx.AsyncClient | None` rather than
gaining a `Union` import. requires-python is >=3.10, so PEP 604 unions
evaluate at runtime, and UP007 and UP045 both sit at a ceiling of 0 in
ruff-strict-budget.json, so importing `Union` would trade the F821 for a
budget failure.
@yassin-berriai
yassin-berriai force-pushed the litellm_fix_openai_common_utils_union branch from cc81916 to 256f1ec Compare August 3, 2026 20:57
@greptile-apps

greptile-apps Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR restores LiteLLM importability by replacing an annotation that references the missing Union name with supported PEP 604 syntax.

  • Updates BaseOpenAILLM.owns_wrapped_http_client to accept sync clients, async clients, or None using |.
  • Preserves the annotation’s existing type semantics while avoiding the import-time NameError.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
litellm/llms/openai/common_utils.py Replaces the undefined Union reference with an equivalent annotation supported by the package’s Python 3.10+ runtime requirement.

Reviews (2): Last reviewed commit: "fix(openai): repair the annotation that ..." | Re-trigger Greptile

@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai please review the current head 256f1ec

The 5/5 above is pinned to cc81916, which was force-pushed away. That commit added Union to the typing import and traded the F821 for a lint failure on UP007: total 67 over limit 0, since UP007 and UP045 both sit at a ceiling of 0. The head instead rewrites the annotation as httpx.Client | httpx.AsyncClient | None; requires-python is >=3.10 so the PEP 604 union evaluates at runtime, and the strict-rule gate passes locally against the current base.

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed-hq

codspeed-hq Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_fix_openai_common_utils_union (256f1ec) with litellm_internal_staging (7c8364c)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_internal_staging (9d5984b) during the generation of this report, so 7c8364c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@yassin-berriai yassin-berriai changed the title fix(openai): import Union in common_utils so litellm is importable fix(openai): repair the annotation that makes litellm unimportable Aug 3, 2026
@yassin-berriai

Copy link
Copy Markdown
Contributor Author

Superseded by #35706, which landed the identical fix (httpx.Client | httpx.AsyncClient | None rather than a Union import, for the same UP007/UP045 ceiling reason) and also covers a second call site in openai.py that this PR missed. Staging imports again; closing.

@yassin-berriai
yassin-berriai deleted the litellm_fix_openai_common_utils_union branch August 3, 2026 22:35
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