2222
2323
2424class BedrockResponse :
25- """
26- Response class for Bedrock Agents Lambda functions.
27-
28- Parameters
29- ----------
30- status_code: int
31- HTTP status code for the response
32- body: Any
33- Response body content
34- content_type: str, optional
35- Content type of the response (default: application/json)
36- session_attributes: dict[str, Any], optional
37- Session attributes to maintain state
38- prompt_session_attributes: dict[str, Any], optional
39- Prompt-specific session attributes
40- knowledge_bases_configuration: dict[str, Any], optional
41- Knowledge base configuration settings
42- """
43-
4425 def __init__ (
4526 self ,
46- status_code : int ,
4727 body : Any ,
28+ status_code : int = 200 ,
4829 content_type : str = "application/json" ,
4930 session_attributes : dict [str , Any ] | None = None ,
5031 prompt_session_attributes : dict [str , Any ] | None = None ,
51- knowledge_bases_configuration : dict [str , Any ] | None = None ,
32+ knowledge_bases_configuration : list [ dict [str , Any ] ] | None = None ,
5233 ) -> None :
53- self .status_code = status_code
5434 self .body = body
35+ self .status_code = status_code
5536 self .content_type = content_type
5637 self .session_attributes = session_attributes
5738 self .prompt_session_attributes = prompt_session_attributes
39+
40+ if knowledge_bases_configuration is not None :
41+ if not isinstance (knowledge_bases_configuration , list ) or not all (
42+ isinstance (item , dict ) for item in knowledge_bases_configuration
43+ ):
44+ raise ValueError ("knowledge_bases_configuration must be a list of dictionaries" )
45+
5846 self .knowledge_bases_configuration = knowledge_bases_configuration
5947
48+ def to_dict (self , event ) -> dict [str , Any ]:
49+ result = {
50+ "messageVersion" : "1.0" ,
51+ "response" : {
52+ "apiPath" : event .api_path ,
53+ "actionGroup" : event .action_group ,
54+ "httpMethod" : event .http_method ,
55+ "httpStatusCode" : self .status_code ,
56+ "responseBody" : {
57+ self .content_type : {"body" : json .dumps (self .body ) if isinstance (self .body , dict ) else self .body },
58+ },
59+ },
60+ }
61+
62+ # Add optional attributes if they exist
63+ if self .session_attributes is not None :
64+ result ["sessionAttributes" ] = self .session_attributes
65+
66+ if self .prompt_session_attributes is not None :
67+ result ["promptSessionAttributes" ] = self .prompt_session_attributes
68+
69+ if self .knowledge_bases_configuration is not None :
70+ result ["knowledgeBasesConfiguration" ] = self .knowledge_bases_configuration
71+
72+ return result
73+
6074
6175class BedrockResponseBuilder (ResponseBuilder ):
6276 """
@@ -72,6 +86,10 @@ def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
7286 """
7387 self ._route (event , None )
7488
89+ body = self .response .body
90+ if self .response .is_json () and not isinstance (self .response .body , str ):
91+ body = self .serializer (self .response .body )
92+
7593 base_response = {
7694 "messageVersion" : "1.0" ,
7795 "response" : {
@@ -81,7 +99,7 @@ def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
8199 "httpStatusCode" : self .response .status_code ,
82100 "responseBody" : {
83101 self .response .content_type : {
84- "body" : self . _get_formatted_body () ,
102+ "body" : body ,
85103 },
86104 },
87105 },
@@ -92,22 +110,7 @@ def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
92110
93111 return base_response
94112
95- def _get_formatted_body (self ) -> Any :
96- """Format the response body based on content type"""
97- if not isinstance (self .response , BedrockResponse ):
98- if self .response .is_json () and not isinstance (self .response .body , str ):
99- return self .serializer (self .response .body )
100- return self .response .body
101-
102113 def _add_bedrock_specific_configs (self , response : dict [str , Any ]) -> None :
103- """
104- Add Bedrock-specific configurations to the response if present.
105-
106- Parameters
107- ----------
108- response: dict[str, Any]
109- The base response dictionary to be updated
110- """
111114 if not isinstance (self .response , BedrockResponse ):
112115 return
113116
0 commit comments