@@ -454,21 +454,38 @@ def _add_instructions_event(
454454 additional_instructions : Optional [str ],
455455 agent_id : Optional [str ] = None ,
456456 thread_id : Optional [str ] = None ,
457+ response_schema : Optional [Any ] = None ,
457458 ) -> None :
458- # Early return if no instructions to trace
459- if not instructions :
459+ # Early return if no instructions AND no response schema to trace
460+ if not instructions and response_schema is None :
460461 return
461462
462463 content_array : List [Dict [str , Any ]] = []
463464 if _trace_agents_content :
464- # Combine instructions if both exist
465- if additional_instructions :
466- combined_text = f"{ instructions } { additional_instructions } "
467- else :
468- combined_text = instructions
465+ # Add instructions if provided
466+ if instructions :
467+ # Combine instructions if both exist
468+ if additional_instructions :
469+ combined_text = f"{ instructions } { additional_instructions } "
470+ else :
471+ combined_text = instructions
472+
473+ # Use optimized format with consistent "content" field
474+ content_array .append ({"type" : "text" , "content" : combined_text })
475+
476+ # Add response schema if provided
477+ if response_schema is not None :
478+ # Convert schema to JSON string if it's a dict/object
479+ if isinstance (response_schema , dict ):
480+ schema_str = json .dumps (response_schema , ensure_ascii = False )
481+ elif hasattr (response_schema , "__dict__" ):
482+ # Handle model objects by converting to dict first
483+ schema_dict = {k : v for k , v in response_schema .__dict__ .items () if not k .startswith ("_" )}
484+ schema_str = json .dumps (schema_dict , ensure_ascii = False )
485+ else :
486+ schema_str = str (response_schema )
469487
470- # Use optimized format with consistent "content" field
471- content_array .append ({"type" : "text" , "content" : combined_text })
488+ content_array .append ({"type" : "response_schema" , "content" : schema_str })
472489
473490 attributes = self ._create_event_attributes (agent_id = agent_id , thread_id = thread_id )
474491 # Store as JSON array directly without outer wrapper
@@ -536,8 +553,21 @@ def start_create_agent_span( # pylint: disable=too-many-locals
536553 if agent_type :
537554 span .add_attribute ("gen_ai.agent.type" , agent_type )
538555
556+ # Extract response schema from text parameter if available
557+ response_schema = None
558+ if response_format and text :
559+ # Extract schema from text.format.schema if available
560+ if hasattr (text , "format" ):
561+ format_info = getattr (text , "format" , None )
562+ if format_info and hasattr (format_info , "schema" ):
563+ response_schema = getattr (format_info , "schema" , None )
564+ elif isinstance (text , dict ):
565+ format_info = text .get ("format" )
566+ if format_info and isinstance (format_info , dict ):
567+ response_schema = format_info .get ("schema" )
568+
539569 # Add instructions event (if instructions exist)
540- self ._add_instructions_event (span , instructions , None )
570+ self ._add_instructions_event (span , instructions , None , response_schema = response_schema )
541571
542572 # Add workflow event if workflow type agent (always add event, but only include YAML content if content recording enabled)
543573 if workflow_yaml is not None :
0 commit comments