fix: Cohere usage parsing and remove deprecated models#52
Conversation
- Fix usage parsing to use 'usage' field instead of 'meta' in API response - Remove deprecated 'command-r' and 'command-r-plus' models - Update streaming stream-end event handler to use correct field name - Fixes issue where input_tokens, output_tokens, and total_tokens were None
Code Review: Cohere Usage Parsing Fix and Deprecated Models RemovalSummaryThis PR addresses two important issues:
✅ Positive AspectsCode Quality
Bug Fix
Model Removal
🔍 Observations & Suggestions1. Test Coverage Recommendations:
2. API Response Validation # In streaming.py:66 and 97
usage_dict = delta.get("usage", {}) # Could be message-end event
usage_data = event.get("usage", {}) # stream-end eventThis inconsistency in naming ( 3. Deprecated Models Deprecation Date This is a future date (we're in November 2025 according to context, but September 2025 is still odd). Did you mean 2024? Please verify the actual deprecation date. 4. Duplication in Streaming Code
Suggestion: Consider extracting this into a shared helper method to reduce duplication and improve maintainability: def _parse_usage_from_dict(usage_data: dict[str, Any]) -> TextGenerationUsage | None:
"""Parse usage from a usage data dictionary."""
if not isinstance(usage_data, dict):
return None
billed_units = usage_data.get("billed_units", {})
tokens = usage_data.get("tokens", {})
input_tokens = billed_units.get("input_tokens")
output_tokens = billed_units.get("output_tokens")
if input_tokens is not None or output_tokens is not None:
return TextGenerationUsage(
input_tokens=input_tokens,
output_tokens=output_tokens,
total_tokens=tokens.get("total_tokens") if tokens else None,
cached_tokens=usage_data.get("cached_tokens"),
)
return None5. Migration Path
🔒 SecurityNo security concerns identified. The changes don't introduce any new attack vectors. ⚡ PerformanceNo performance concerns. The changes are neutral from a performance perspective. 🐛 Potential BugsThe code looks correct for the intended fix. However:
📋 Action ItemsCritical:
Recommended: VerdictThe code changes are correct and well-implemented, but the PR would benefit significantly from:
Overall: Approve with suggestions - The bug fix is necessary and correct, but please add tests before merging. |
Changes
Fix usage parsing bug: Changed from looking for
metafield tousagefield in Cohere API responseinput_tokens,output_tokens, andtotal_tokenswereNoneeven when data existedRemove deprecated models: Removed
command-randcommand-r-plusmodels that were deprecated by Cohere on September 15, 2025command-a-03-2025orcommand-r7b-12-2024insteadFiles Changed
packages/text-generation/src/celeste_text_generation/providers/cohere/client.pypackages/text-generation/src/celeste_text_generation/providers/cohere/models.pypackages/text-generation/src/celeste_text_generation/providers/cohere/streaming.py