mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fix runtime response format for responses client (#2440)
* Fix runtime response format for responses client * Handle run time schema for Azure AI Client.
This commit is contained in:
committed by
GitHub
Unverified
parent
e303cc963d
commit
2a4802eac3
@@ -31,6 +31,7 @@ This folder contains examples demonstrating different ways to create and use age
|
||||
| [`openai_responses_client_with_function_tools.py`](openai_responses_client_with_function_tools.py) | Demonstrates how to use function tools with agents. Shows both agent-level tools (defined when creating the agent) and run-level tools (provided with specific queries). |
|
||||
| [`openai_responses_client_with_hosted_mcp.py`](openai_responses_client_with_hosted_mcp.py) | Shows how to integrate OpenAI agents with hosted Model Context Protocol (MCP) servers, including approval workflows and tool management for remote MCP services. |
|
||||
| [`openai_responses_client_with_local_mcp.py`](openai_responses_client_with_local_mcp.py) | Shows how to integrate OpenAI agents with local Model Context Protocol (MCP) servers for enhanced functionality and tool integration. |
|
||||
| [`openai_responses_client_with_runtime_json_schema.py`](openai_responses_client_with_runtime_json_schema.py) | Shows how to supply a runtime JSON Schema via `additional_chat_options` for structured output without defining a Pydantic model. |
|
||||
| [`openai_responses_client_with_structured_output.py`](openai_responses_client_with_structured_output.py) | Demonstrates how to use structured outputs with OpenAI agents to get structured data responses in predefined formats. |
|
||||
| [`openai_responses_client_with_thread.py`](openai_responses_client_with_thread.py) | Demonstrates thread management with OpenAI agents, including automatic thread creation for stateless conversations and explicit thread management for maintaining conversation context across multiple interactions. |
|
||||
| [`openai_responses_client_with_web_search.py`](openai_responses_client_with_web_search.py) | Shows how to use web search capabilities with OpenAI agents to retrieve and use information from the internet in responses. |
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ async def streaming_example() -> None:
|
||||
query = "Give a brief weather digest for Portland."
|
||||
print(f"User: {query}")
|
||||
|
||||
chunks = []
|
||||
chunks: list[str] = []
|
||||
async for chunk in agent.run_stream(
|
||||
query,
|
||||
additional_chat_options={
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
|
||||
"""
|
||||
OpenAI Chat Client Runtime JSON Schema Example
|
||||
|
||||
Demonstrates structured outputs when the schema is only known at runtime.
|
||||
Uses additional_chat_options to pass a JSON Schema payload directly to OpenAI
|
||||
without defining a Pydantic model up front.
|
||||
"""
|
||||
|
||||
|
||||
runtime_schema = {
|
||||
"title": "WeatherDigest",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {"type": "string"},
|
||||
"conditions": {"type": "string"},
|
||||
"temperature_c": {"type": "number"},
|
||||
"advisory": {"type": "string"},
|
||||
},
|
||||
# OpenAI strict mode requires every property to appear in required.
|
||||
"required": ["location", "conditions", "temperature_c", "advisory"],
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
|
||||
async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
|
||||
query = "Give a brief weather digest for Seattle."
|
||||
print(f"User: {query}")
|
||||
|
||||
response = await agent.run(
|
||||
query,
|
||||
additional_chat_options={
|
||||
"response_format": {
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
"name": runtime_schema["title"],
|
||||
"strict": True,
|
||||
"schema": runtime_schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
print("Model output:")
|
||||
print(response.text)
|
||||
|
||||
parsed = json.loads(response.text)
|
||||
print("Parsed dict:")
|
||||
print(parsed)
|
||||
|
||||
|
||||
async def streaming_example() -> None:
|
||||
print("=== Streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
|
||||
query = "Give a brief weather digest for Portland."
|
||||
print(f"User: {query}")
|
||||
|
||||
chunks: list[str] = []
|
||||
async for chunk in agent.run_stream(
|
||||
query,
|
||||
additional_chat_options={
|
||||
"response_format": {
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
"name": runtime_schema["title"],
|
||||
"strict": True,
|
||||
"schema": runtime_schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
):
|
||||
if chunk.text:
|
||||
chunks.append(chunk.text)
|
||||
|
||||
raw_text = "".join(chunks)
|
||||
print("Model output:")
|
||||
print(raw_text)
|
||||
|
||||
parsed = json.loads(raw_text)
|
||||
print("Parsed dict:")
|
||||
print(parsed)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
print("=== OpenAI Chat Client with runtime JSON Schema ===")
|
||||
|
||||
await non_streaming_example()
|
||||
await streaming_example()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user