fix(langfuse): Handle null usage values to prevent validation errors #16396
+137
−33
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.
Handle null usage values to prevent validation errors
Relevant issues
Fixes #16330
Pre-Submission checklist
Please complete all items before asking a Lite-LLM maintainer to review your PR
tests/litellm/integrations/test_langfuse.pydirectory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🐛 Bug Fix
✅ Test
Changes
This pull request resolves a
pydantic.ValidationErrorthat occurred when sending data to the Langfuse callback.Problem:
When an upstream provider (e.g., an OpenAI-compatible endpoint) returned a
usageobject withnullvalues for token counts, theseNonevalues were passed directly to the Langfuse client, which requires integer values for these fields.Solution:
The
_log_langfuse_v2method inlitellm/integrations/langfuse/langfuse.pyhas been updated to robustly handleNonevalues for all token fields (prompt_tokens,completion_tokens,total_tokens, etc.). It now coalesces anyNonevalue to0before constructing theusageandusage_detailspayloads for Langfuse. This ensures the data is always compliant with the Langfuse schema.Testing:
A new unit test,
test_log_langfuse_v2_handles_null_usage_values, has been added totests/test_litellm/integrations/test_langfuse.py. This test:Nonevalues in theusageobject.0.unittest.mock.patchto avoid side effects and ensure it only tests the intended logic, consistent with the existing testing style in the file.