@@ -2,7 +2,7 @@ use crate::prelude::*;
22use base64:: prelude:: * ;
33
44use crate :: llm:: {
5- LlmGenerateRequest , LlmGenerateResponse , LlmGenerationClient , OutputFormat ,
5+ GeneratedOutput , LlmGenerateRequest , LlmGenerateResponse , LlmGenerationClient , OutputFormat ,
66 ToJsonSchemaOptions , detect_image_mime_type,
77} ;
88use anyhow:: Context ;
@@ -83,6 +83,7 @@ impl LlmGenerationClient for Client {
8383 }
8484
8585 // Handle structured output using tool schema
86+ let has_json_schema = request. output_format . is_some ( ) ;
8687 if let Some ( OutputFormat :: JsonSchema { schema, name } ) = request. output_format . as_ref ( ) {
8788 let schema_json = serde_json:: to_value ( schema) ?;
8889 payload[ "toolConfig" ] = serde_json:: json!( {
@@ -134,7 +135,7 @@ impl LlmGenerationClient for Client {
134135 let message = & output[ "message" ] ;
135136 let content = & message[ "content" ] ;
136137
137- let text = if let Some ( content_array) = content. as_array ( ) {
138+ let generated_output = if let Some ( content_array) = content. as_array ( ) {
138139 // Look for tool use first (structured output)
139140 let mut extracted_json: Option < serde_json:: Value > = None ;
140141 for item in content_array {
@@ -148,7 +149,19 @@ impl LlmGenerationClient for Client {
148149
149150 if let Some ( json) = extracted_json {
150151 // Return the structured output as JSON
151- serde_json:: to_string ( & json) ?
152+ GeneratedOutput :: Json ( json)
153+ } else if has_json_schema {
154+ // If JSON schema was requested but no tool output found, try parsing text as JSON
155+ let mut text_parts = Vec :: new ( ) ;
156+ for item in content_array {
157+ if let Some ( text) = item. get ( "text" ) {
158+ if let Some ( text_str) = text. as_str ( ) {
159+ text_parts. push ( text_str) ;
160+ }
161+ }
162+ }
163+ let text = text_parts. join ( "" ) ;
164+ GeneratedOutput :: Json ( serde_json:: from_str ( & text) ?)
152165 } else {
153166 // Fall back to text content
154167 let mut text_parts = Vec :: new ( ) ;
@@ -159,13 +172,15 @@ impl LlmGenerationClient for Client {
159172 }
160173 }
161174 }
162- text_parts. join ( "" )
175+ GeneratedOutput :: Text ( text_parts. join ( "" ) )
163176 }
164177 } else {
165178 return Err ( anyhow:: anyhow!( "No content found in Bedrock response" ) ) ;
166179 } ;
167180
168- Ok ( LlmGenerateResponse { text } )
181+ Ok ( LlmGenerateResponse {
182+ output : generated_output,
183+ } )
169184 }
170185
171186 fn json_schema_options ( & self ) -> ToJsonSchemaOptions {
0 commit comments