Repository navigation
Commit 43a6312
Python: fix(anthropic): map every Anthropic stop reason instead of raising KeyError (#14616)
### Motivation and Context
`AnthropicChatCompletion` fails whenever Claude ends a reply with a stop
reason other than `end_turn`, `max_tokens` or `tool_use`.
`ANTHROPIC_TO_SEMANTIC_KERNEL_FINISH_REASON_MAP` only has those three
and is indexed with `[...]`, while the Anthropic API also returns
`stop_sequence` (whenever one of the request's `stop_sequences` ends the
reply), `refusal`, `pause_turn` and `model_context_window_exceeded`. For
example, any call with `stop_sequences` that stops on one of them:
- non-streaming: `get_chat_message_content` raises `KeyError:
'stop_sequence'`;
- streaming: `get_streaming_chat_message_content` raises
`ServiceResponseException`, caused by the same `KeyError`.
### Description
- `stop_sequence` → `FinishReason.STOP`, `model_context_window_exceeded`
→ `FinishReason.LENGTH`, `refusal` → `FinishReason.CONTENT_FILTER`.
- Both lookups (`_create_chat_message_content` and
`_create_streaming_chat_message_content`) use `.get(...)`, so a stop
reason without a Semantic Kernel equivalent (`pause_turn`, or one added
later) leaves `finish_reason` unset instead of failing the call.
Reproduced end to end with `AnthropicChatCompletion` and a real
`AsyncAnthropic` client against a local endpoint that returns
`stop_reason: "stop_sequence"`: before, both calls raise; after, the
non-streaming call returns the message with `FinishReason.STOP` and the
stream completes.
### Contribution Checklist
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
Tests: `test_stop_reason_maps_to_finish_reason` in
`tests/unit/connectors/ai/anthropic/services/test_anthropic_chat_completion.py`,
parametrized over all seven Anthropic stop reasons, checks both the
streaming and non-streaming conversion. The four unmapped ones fail on
`main` with `KeyError`. `tests/unit/connectors/ai/anthropic`: 42 passed.
`ruff format`, `ruff check` and `mypy` on the changed module are clean.
This change was written with Claude Code; the tests and the reproduction
above were run before opening the PR.
Co-authored-by: Claude Opus 5.5 <[email protected]>1 parent 8d46973 commit 43a6312
2 files changed
Lines changed: 43 additions & 2 deletions
File tree
- python
- semantic_kernel/connectors/ai/anthropic/services
- tests/unit/connectors/ai/anthropic/services
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
68 | 69 | | |
| 70 | + | |
69 | 71 | | |
| 72 | + | |
70 | 73 | | |
71 | 74 | | |
72 | 75 | | |
| |||
274 | 277 | | |
275 | 278 | | |
276 | 279 | | |
277 | | - | |
| 280 | + | |
278 | 281 | | |
279 | 282 | | |
280 | 283 | | |
| |||
312 | 315 | | |
313 | 316 | | |
314 | 317 | | |
315 | | - | |
| 318 | + | |
316 | 319 | | |
317 | 320 | | |
318 | 321 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
666 | 667 | | |
667 | 668 | | |
668 | 669 | | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
0 commit comments