- Replace full usage dict logging with keys and type only
- Prevents potential exposure of sensitive information in debug logs
- Maintains debugging capability without security risk
- Fix ID extraction to preserve falsy values (0, empty string, False) from response_obj
- Only fallback to litellm_call_id when id is None (attribute doesn't exist)
- Remove redundant comments that duplicate function names and obvious logic
- Clean up code for better readability
- Add _safe_model_dump() for safe model_dump() calls with fallbacks
- Add _safe_get_attribute() for safe attribute access from dict/BaseModel
- Add _safe_extract_usage_from_obj() for safe usage extraction
- Add _try_transform_response_api_usage() for ResponseAPIUsage transformation
- Add _try_create_usage_from_dict() for safe Usage creation
- Refactor get_usage_from_response_obj() to use helper functions
- Refactor _extract_response_obj_and_hidden_params() to use helper functions
- Refactor get_final_response_obj() to use helper functions
- Update type hints to support Union[dict, BaseModel] throughout
- Restore original defensive behavior: return Usage(0,0,0) for unknown types instead of raising ValueError
- Keep explicit ResponseAPIUsage handling to fix the bug where it was incorrectly returning 0 tokens
- Maintain backward compatibility by preserving original error handling approach
- Fix bug where ResponseAPIUsage objects from ResponsesAPIResponse were not being transformed
- Add explicit handling for ResponseAPIUsage objects (not just dicts)
- Use _is_response_api_usage() helper to handle both ResponseAPIUsage objects and dicts
- Remove problematic 'or {}' fallback that was converting None to empty dict
This fixes the test failure where prompt_tokens was 0 instead of 8 for ResponsesAPIResponse objects.
- Return BaseModel directly instead of converting to dict via model_dump()
- Update get_usage_from_response_obj to handle both dict and BaseModel
- Update get_final_response_obj to handle BaseModel (lazy conversion)
- Add type guards for direct attribute access when response_obj is BaseModel
This optimization eliminates the expensive model_dump() call (previously 92.6% of function time)
by leveraging ModelResponse's .get() method and deferring dict conversion until needed.
* Optimize _get_model_cost_key to avoid expensive scans
- Remove expensive O(n) scan fallback that was causing 42.87% CPU overhead
- Only scan when size mismatch detected (O(1) check)
- Add warning in docstring: Only O(1) lookup operations are acceptable
- Clean up comments to be more concise
- Keep stale entry rebuild for pop() case (only triggers when stale entry found)
This fixes the performance issue where the scan was being triggered on every
failed lookup, causing severe CPU overhead during router operations.
* Add code quality check to enforce O(1) operations in _get_model_cost_key
- Add check_get_model_cost_key_performance.py to statically analyze _get_model_cost_key
- Detects O(n) operations (loops, comprehensions, problematic function calls)
- Recursively checks called functions to find nested O(n) operations
- Allows conditional O(n) rebuilds in helper functions (_rebuild_model_cost_lowercase_map, _handle_stale_map_entry_rebuild, _handle_new_key_with_scan)
* Integrate _get_model_cost_key performance check into CI pipeline
- Add check_get_model_cost_key_performance.py to check_code_and_doc_quality job
- Ensures O(1) requirement is enforced in CI to prevent performance regressions
* Remove unused performance test and clean up utils.py
- Remove test_get_model_info_performance.py (no longer needed)
- Remove extra blank line in utils.py
* Document allowed helper functions and exception process in _get_model_cost_key
- Add documentation listing allowed helper functions with O(n) operations
- Explain why these are acceptable (conditionally called)
- Add instructions for adding new exceptions to check_get_model_cost_key_performance.py
* Fix docstring detection and type checker error in performance check
- Add proper docstring tracking to skip docstring content (fixes false positive for 'map' in docstring)
- Add None check for docstring_quote to fix type checker error
- Restore _handle_new_key_with_scan to allowed_helpers list
* Remove check_get_model_cost_key_performance from CI pipeline
- Temporarily remove the performance check from CI to avoid blocking builds
* Restore performance check and remove memory leak tests from CI
- Add back check_get_model_cost_key_performance.py to CI pipeline
- Remove memory_leak_tests job that was causing port conflicts
* Remove extra blank line in CI config
* do not fallback to token counter if disable_token_counter is enabled, and return errors instead
* add exceptions and exception utils to map the same as /v1/chat/completions
* use safe_json_loads