Commit a1c0938
feat: map LiteLLM finish_reason strings to FinishReason enum
Maps LiteLLM finish_reason string values to proper FinishReason enum
for type consistency with Gemini native responses.
Changes:
- Add _FINISH_REASON_MAPPING dictionary for string->enum conversion
- "length" -> FinishReason.MAX_TOKENS
- "stop" -> FinishReason.STOP
- "tool_calls"/"function_call" -> FinishReason.STOP
- "content_filter" -> FinishReason.SAFETY
- Unknown values -> FinishReason.OTHER (fallback)
- Update finish_reason type hint to Optional[FinishReason] (no Union needed)
- Update telemetry tracing to use .name for enum serialization
- Add explanatory comments:
- Why tool_calls maps to STOP (no TOOL_CALL enum exists)
- Docstring clarifies mapping applies to all model providers
Tests:
- test_finish_reason_propagation: verifies enum mapping for all values
- test_finish_reason_unknown_maps_to_other: verifies fallback behavior
Benefits:
- Type consistency: finish_reason is always FinishReason enum
- No runtime warnings from mixed types
- Enables proper isinstance() checks in callbacks
- Dictionary mapping improves maintainability
- Better integration with ADK telemetry1 parent 7996112 commit a1c0938
3 files changed
Lines changed: 20 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
67 | 80 | | |
68 | 81 | | |
69 | 82 | | |
| |||
508 | 521 | | |
509 | 522 | | |
510 | 523 | | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
523 | 527 | | |
524 | 528 | | |
525 | 529 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
309 | | - | |
| 309 | + | |
310 | 310 | | |
311 | 311 | | |
312 | 312 | | |
| |||
0 commit comments