Skip to content

Commit 8884ad7

Browse files
authored
add tracing support for structured output agent creation (#44192)
* add tracing support for structured output agent creation * updating assets.json * a change to re-trigger ci build
1 parent 8bbaba5 commit 8884ad7

File tree

5 files changed

+686
-11
lines changed

5 files changed

+686
-11
lines changed

sdk/ai/azure-ai-projects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features Added
66

77
* The package now takes dependency on openai and azure-identity packages. No need to install them separately.
8+
* Tracing: support for tracing the schema when agent is created with structured output definition.
89

910
### Breaking changes
1011

sdk/ai/azure-ai-projects/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/ai/azure-ai-projects",
5-
"Tag": "python/ai/azure-ai-projects_6895b440ac"
5+
"Tag": "python/ai/azure-ai-projects_354ab8e34b"
66
}

sdk/ai/azure-ai-projects/azure/ai/projects/telemetry/_ai_project_instrumentor.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)