Repository navigation
fix(sdk): add defensive safeguards to litellm handler and lazy assets loading - #6979
truecallerabreham wants to merge 3 commits into
Conversation
|
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository YAML (base), Organization UI (inherited) Review profile: CHILL Plan: Advanced Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review. 📝 SummarySummary by CodeRabbit
WalkthroughThe LiteLLM handler now handles missing spans, optional exceptions, and multiple input formats in synchronous and asynchronous callbacks. LiteLLM cost calculation imports are optional. Unit tests cover these cases. ChangesLiteLLM robustness
Priority: ⬇️ Low Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Advanced
Run ID: 4c1f29b5-461c-4fb6-9ebd-fd32a6147270
⛔ Files ignored due to path filters (1)
sdks/python/oss/tests/pytest/unit/test_litellm_results.svgis excluded by!**/*.svg
📒 Files selected for processing (3)
sdks/python/agenta/sdk/litellm/litellm.pysdks/python/agenta/sdk/utils/assets.pysdks/python/oss/tests/pytest/unit/test_litellm_handler.py
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.
Summary
This PR fixes multiple unhandled exceptions in the Python SDK's LiteLLM integration (
sdks/python/agenta/sdk/litellm/litellm.py) and resolves broken lazy loading inassets.py:TypeError: 'NoneType' object is not a mapping: Inlog_pre_api_call,**kwargs.get("optional_params")crashed whenoptional_paramswas absent orNone. It now defaults safely to{}.KeyError: 'messages'for non-chat operations: Inlog_pre_api_call, accessingkwargs["messages"]crashed on embedding or completion calls where inputs are passed under"prompt"or"input". Input extraction now safely falls back acrosskwargs.get("messages"),messages,kwargs.get("prompt"), andkwargs.get("input").KeyErroron span lookups: Inlog_stream_event,log_success_event,log_failure_event, and their async variants,self.span[litellm_call_id]raised an unhandledKeyErrorif a span was untracked orlog_pre_api_callfailed, bypassing the intendedif not span:guard. Switched toself.span.get(litellm_call_id).KeyErroron span deletion:del self.span[litellm_call_id]crashed if already cleaned up or untracked. Replaced withself.span.pop(litellm_call_id, None).KeyError: 'exception':span.record_exception(kwargs["exception"])crashed ifkwargsdid not contain"exception". Added safe key andNonecheck.assets.py: Top-levelfrom litellm import cost_calculatorbroke lazy loading across the SDK and caused any import ofagentato crash whenlitellmwas not installed. Wrapped intry/except Exception: cost_calculator = None.Testing
Verified locally
test_message_types.py,test_preinit_warnings.py,test_redaction.py,test_redaction_seed.py,test_structured_rendering.py) — total 79 tests passing with 0 regressions.Added or updated tests
sdks/python/oss/tests/pytest/unit/test_litellm_handler.pycontaining 10 automated test cases testing sync and async callback safety, missing/None optional params, embedding calls without messages, missing exceptions, and unknown call IDs.QA follow-up
Demo
Checklist