Skip to content

Commit c28d64d

Browse files
Python: fix(bedrock): map guardrail_intervened and model_context_window_exceeded stop reasons (#14631)
### Motivation and Context The Bedrock Converse API can end a turn with `guardrail_intervened` (a configured guardrail blocked the output) or `model_context_window_exceeded` (the model hit its context window). `finish_reason_from_bedrock_to_semantic_kernel` only knew five of the stop reasons, so for these two the message came back with `finish_reason=None`, indistinguishable from an unrecognised value. ### Description - `guardrail_intervened` maps to `FinishReason.CONTENT_FILTER`, same as `content_filtered`. - `model_context_window_exceeded` maps to `FinishReason.LENGTH`, same as `max_tokens`. - Unit tests for both mappings; they fail on `main` (the function returns `None`) and pass with the change. The Bedrock unit test folder passes. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the SK Contribution Guidelines - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 This change was made with Claude Code; the tests were run against `main` and the branch.
1 parent 2ebeb95 commit c28d64d

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎python/semantic_kernel/connectors/ai/bedrock/services/model_provider/utils.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,7 @@ def finish_reason_from_bedrock_to_semantic_kernel(finish_reason: str) -> FinishR
213213
"end_turn": FinishReason.STOP,
214214
"max_tokens": FinishReason.LENGTH,
215215
"content_filtered": FinishReason.CONTENT_FILTER,
216+
"guardrail_intervened": FinishReason.CONTENT_FILTER,
217+
"model_context_window_exceeded": FinishReason.LENGTH,
216218
"tool_use": FinishReason.TOOL_CALLS,
217219
}.get(finish_reason)

‎python/tests/unit/connectors/ai/bedrock/services/test_bedrock_model_provider_utils.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ def test_finish_reason_from_bedrock_to_semantic_kernel_tool_use() -> None:
344344
assert reason == FinishReason.TOOL_CALLS
345345

346346

347+
def test_finish_reason_from_bedrock_to_semantic_kernel_guardrail_intervened() -> None:
348+
"""Test that 'guardrail_intervened' maps to FinishReason.CONTENT_FILTER"""
349+
reason = finish_reason_from_bedrock_to_semantic_kernel("guardrail_intervened")
350+
assert reason == FinishReason.CONTENT_FILTER
351+
352+
353+
def test_finish_reason_from_bedrock_to_semantic_kernel_context_window_exceeded() -> None:
354+
"""Test that 'model_context_window_exceeded' maps to FinishReason.LENGTH"""
355+
reason = finish_reason_from_bedrock_to_semantic_kernel("model_context_window_exceeded")
356+
assert reason == FinishReason.LENGTH
357+
358+
347359
def test_finish_reason_from_bedrock_to_semantic_kernel_unknown() -> None:
348360
"""Test that unknown finish reason returns None"""
349361
reason = finish_reason_from_bedrock_to_semantic_kernel("something_unknown")

0 commit comments

Comments
 (0)