refactor: migrate Anthropic to native structured outputs API and remove raw_event metadata#40
Conversation
…ve raw_event metadata
This commit includes two major improvements:
1. Anthropic Structured Outputs Migration:
- Migrate from tool-based structured outputs to Anthropic's native structured outputs API
- Use output_format parameter with json_schema type instead of tools/tool_choice
- Simplify streaming implementation by removing tool_use block tracking
- Remove complex tool_use parsing logic (203 lines removed from streaming.py)
- Add anthropic-beta header for structured-outputs-2025-11-13 feature
- Update OutputSchemaMapper to use native output_format instead of tools
- Remove OUTPUT_SCHEMA constraint from model definitions (now handled via API)
2. Remove raw_event Metadata:
- Remove metadata={"raw_event": event} from all streaming chunk creations
- Violates metadata principle: metadata should not contain content fields
- Reduces memory usage by not duplicating event data
- Affects: OpenAI, Anthropic, XAI, Cohere, Mistral, Google text generation providers
- Also removes from image intelligence and speech generation providers
- ElevenLabs: removed raw_event wrapper but kept content_length metadata
Benefits:
- Cleaner codebase with 313 lines removed
- Better adherence to metadata design principles
- Reduced memory footprint in streaming scenarios
- Simpler Anthropic integration using official API
Pull Request Review: Migration to Anthropic Native Structured Outputs APIThis PR represents a significant refactoring that modernizes the Anthropic integration and improves code quality. Overall, the changes are well-executed with strong benefits. Here's my detailed review: ✅ Code Quality and Best PracticesExcellent Improvements:
Minor Observations:
🐛 Potential Bugs or IssuesCritical Items to Verify:
⚡ Performance ConsiderationsImprovements:
Potential Optimizations:
🔒 Security ConcernsGood Practices:
Observations:
🧪 Test CoverageConcerns:
📋 Specific RecommendationsHigh Priority:
Medium Priority:
Low Priority:
🎯 SummaryOverall Assessment: ✅ Approve with minor recommendations This is a high-quality refactoring that:
Main concerns:
Recommendation: Merge after addressing the test coverage question and verifying production API compatibility. Great work on this refactoring! The code is much cleaner and more maintainable. 🎉 |
This PR includes two major improvements:
1. Anthropic Structured Outputs Migration
output_formatparameter withjson_schematype instead oftools/tool_choiceanthropic-betaheader forstructured-outputs-2025-11-13featureoutput_formatinstead of tools2. Remove raw_event Metadata
metadata={"raw_event": event}from all streaming chunk creationsBenefits
Testing