Skip to content

Commit d463304

Browse files
committed
add validation response state
1 parent 20bbe9f commit d463304

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

aws_lambda_powertools/event_handler/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
BedrockAgentFunctionResolver,
1717
BedrockFunctionResponse,
1818
)
19-
2019
from aws_lambda_powertools.event_handler.events_appsync.appsync_events import AppSyncEventsResolver
2120
from aws_lambda_powertools.event_handler.lambda_function_url import (
2221
LambdaFunctionUrlResolver,

aws_lambda_powertools/event_handler/bedrock_agent_function.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def __init__(
4343
knowledge_bases: list[dict[str, Any]] | None = None,
4444
response_state: str | None = None,
4545
) -> None:
46+
if response_state is not None and response_state not in ["FAILURE", "REPROMPT"]:
47+
raise ValueError("responseState must be None, 'FAILURE' or 'REPROMPT'")
48+
4649
self.body = body
4750
self.session_attributes = session_attributes
4851
self.prompt_session_attributes = prompt_session_attributes

tests/functional/event_handler/required_dependencies/test_bedrock_agent_functions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,24 @@ def test_resolve_with_no_registered_function():
152152

153153
# THEN the response should contain an error message
154154
assert "Error: 'non_existent_function'" in result["response"]["functionResponse"]["responseBody"]["TEXT"]["body"]
155+
156+
157+
def test_bedrock_function_response_state_validation():
158+
# GIVEN invalid and valid response states
159+
valid_states = [None, "FAILURE", "REPROMPT"]
160+
invalid_state = "INVALID"
161+
162+
# WHEN creating responses with valid states
163+
# THEN no error should be raised
164+
for state in valid_states:
165+
try:
166+
BedrockFunctionResponse(body="test", response_state=state)
167+
except ValueError:
168+
pytest.fail(f"Unexpected ValueError for response_state={state}")
169+
170+
# WHEN creating a response with invalid state
171+
# THEN ValueError should be raised with correct message
172+
with pytest.raises(ValueError) as exc_info:
173+
BedrockFunctionResponse(body="test", response_state=invalid_state)
174+
175+
assert str(exc_info.value) == "responseState must be None, 'FAILURE' or 'REPROMPT'"

0 commit comments

Comments
 (0)