fix(litellm): defer import until model usage #2462
Open
+93
−12
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.
Summary
This defers
litellmimport inlitellm_model.pyso importingagents.extensions.models.litellm_modeldoes not eagerly import LiteLLM.This targets issue #610, where Windows users hit
UnicodeDecodeErrorat module import time.What changed
import litellmwith a lazy loader (_import_litellm) and a module proxy.Why this helps
litellm_modelno longer fails immediately due to LiteLLM import-time issues.Testing
uv run --with ruff ruff check src/agents/extensions/models/litellm_model.py tests/models/test_litellm_deferred_import.py tests/models/test_litellm_user_agent.py tests/models/test_map.pyuv run --with pytest pytest -q tests/models/test_litellm_deferred_import.py tests/models/test_litellm_user_agent.py tests/models/test_map.pyuv run --with litellm --with pytest pytest -q tests/models/test_kwargs_functionality.py tests/models/test_litellm_extra_body.pyuv run --with pytest python -m trace --count --coverdir /tmp/openai_agents_610_trace_1770722012 --module pytest tests/models/test_litellm_deferred_import.py -qTrace output confirms all newly added lazy-import branches were executed.