Skip to content

Spurious errors from litellm due to changed type of response.usage #2113

@kouk

Description

@kouk
  • This is actually a bug report.
  • I am not getting good LLM Results
  • I have tried asking for help in the community on discord or discussions and have not received a response.
  • I have tried searching the documentation and have not found an answer.

What Model are you using?

  • gpt-3.5-turbo
  • gpt-4-turbo
  • gpt-4
  • Other: any

Describe the bug
In #343 code was added (now living in the update_total_usage method of instructor.utils.core) that updates the response object with the total tokens used. However when used in conjunction with Litellm this introduces a bug (discussed in BerriAI/litellm#13672) because the type of response.usage unexpectedly changes from one subclass of OpenAIUsage to another (that lacks some methods that litellm added). There is code in litellm (the Langfuse integration) that depends on the response.usage field having this specific type.

To Reproduce

Run repro.py

$ uvx --with langfuse,instructor[litellm]  --exclude-newer 2026-01-01 python repro.py
python: 3.12.4 (main, Jul 12 2024, 09:00:16) [Clang 15.0.0 (clang-1500.3.9.4)]
instructor: 1.13.0
litellm: 1.80.11
langfuse: 3.11.2
14:16:02 - LiteLLM:WARNING: opentelemetry.py:186 - Proxy Server is not installed. Skipping OpenTelemetry initialization.
14:16:05 - LiteLLM:ERROR: _utils.py:273 - [Arize/Phoenix] Failed to set OpenInference span attributes: 'CompletionUsage' object has no attribute 'get'

Expected behavior
No error

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstatus:needs-investigationIssue needs investigation to determine scopetype:regressionRegression from previous version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions