-
Notifications
You must be signed in to change notification settings - Fork 14
🐛 Fix: Critical Streaming Usage Metadata & Advanced Model Support #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
🐛 Fix: Critical Streaming Usage Metadata & Advanced Model Support #22
Conversation
BUGS_FIXED.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file
GITHUB_ISSUES_TEMPLATES.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file
ISSUE_20_UPDATE.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file
PR_DESCRIPTION.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file
test_streaming_bug.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file
- Remove BUGS_FIXED.md - Remove GITHUB_ISSUES_TEMPLATES.md - Remove ISSUE_20_UPDATE.md - Remove PR_DESCRIPTION.md - Remove test_streaming_bug.py Addresses feedback from @Akshay-Dongare in PR Akshay-Dongare#22
- Fix streaming delta conversion to handle both Delta objects and dicts - Add stream_options for usage tracking in streaming responses - Extract and attach usage metadata to streaming message chunks - Support advanced usage fields (cache tokens, reasoning tokens) - Add comprehensive unit and integration tests - Maintain 100% backward compatibility Fixes streaming usage metadata issues and enables cost optimization features. Addresses core functionality gaps in streaming token usage tracking.
- Add cache_creation support via cache_creation_input_tokens - Add audio token support for both input and output - Ensures complete compliance with OpenAI usage metadata schema - Supports multimodal models with audio token tracking
- Fix existing tests to use dictionary access (UsageMetadata is TypedDict) - Add comprehensive tests for new cache and audio token fields - Test cache_creation_input_tokens support - Test audio_input_tokens and audio_output_tokens support - Add complete schema test matching OpenAI format - All usage metadata tests now pass (7/7) Validates the complete token usage metadata schema: - input_tokens, output_tokens, total_tokens - input_token_details: cache_read, cache_creation, audio - output_token_details: audio, reasoning
This test demonstrates that LangChain's _normalize_messages function
now correctly preserves LiteLLM's official multimodal format instead
of incorrectly transforming it.
The test verifies that:
- LiteLLM format: {'type': 'file', 'file': {'file_data': '...'}} is preserved
- OpenAI format: {'type': 'image_url', 'image_url': {'url': '...'}} works correctly
- Vertex format: with 'format' key is preserved
Fixes the KeyError: 'file' that occurred when LangChain transformed
LiteLLM's format to OpenAI's format, breaking LiteLLM compatibility.
- Remove BUGS_FIXED.md - Remove GITHUB_ISSUES_TEMPLATES.md - Remove ISSUE_20_UPDATE.md - Remove PR_DESCRIPTION.md - Remove test_streaming_bug.py Addresses feedback from @Akshay-Dongare in PR Akshay-Dongare#22
c0de4c6 to
3d6d675
Compare
🐛 Fix: Critical Streaming Usage Metadata & Advanced Model Support
📋 Summary
This PR fixes 6 critical bugs in the langchain-litellm package that affect streaming, usage tracking, and advanced AI model features. The primary fix addresses Issue #20 - missing usage metadata in streaming responses.
🔥 Critical Bugs Fixed
1. Missing Usage Metadata in Streaming Responses (Issue #20)
stream_options={"include_usage": True}and usage extraction logic2. Missing reasoning_content Support for Thinking Models
_convert_dict_to_message3. Streaming Crashes with Dictionary Deltas
4. Tool Call Processing Failures
5. Incomplete Usage Metadata
_create_usage_metadatato extract advanced details6. Async Streaming Reliability
🧪 Testing
Before Fix (Broken):
After Fix (Working):
Advanced Model Testing:
📊 Impact Assessment
🔧 Technical Details
Key Changes in
langchain_litellm/chat_models/litellm.py:Enhanced
_default_params:Fixed
_streammethod:Added reasoning_content support:
Robust delta handling:
Enhanced usage metadata:
🚀 Benefits
🔗 Related Issues
📦 Commits
5201401: feat: Add comprehensive streaming usage metadata supportaf41ab9: feat: add reasoning_content support for thinking-enabled modelsReady for Review 🎉 This PR addresses fundamental issues that affect production usage tracking and advanced AI model features. All fixes maintain backward compatibility while adding essential missing functionality.