Create/Get Agent API - fixes and example improvements (#3246)

This commit is contained in:
Dmytro Struk
2026-01-15 20:36:34 -08:00
committed by GitHub
Unverified
parent 975884f32d
commit b773830e4b
9 changed files with 147 additions and 37 deletions
@@ -33,7 +33,7 @@ async def main() -> None:
agent = await provider.create_agent(
name="ProductMarketerAgent",
instructions="Return launch briefs as structured JSON.",
# Specify type to use as response
# Specify Pydantic model for structured output via default_options
default_options={"response_format": ReleaseBrief},
)
@@ -37,17 +37,19 @@ async def main() -> None:
AzureCliCredential() as credential,
AzureAIProjectAgentProvider(credential=credential) as provider,
):
# Pass response_format at agent creation time using dict schema format
# Pass response_format via default_options using dict schema format
agent = await provider.create_agent(
name="WeatherDigestAgent",
instructions="Return sample weather digest as structured JSON.",
response_format={
"type": "json_schema",
"json_schema": {
"name": runtime_schema["title"],
"strict": True,
"schema": runtime_schema,
},
default_options={
"response_format": {
"type": "json_schema",
"json_schema": {
"name": runtime_schema["title"],
"strict": True,
"schema": runtime_schema,
},
}
},
)
@@ -9,8 +9,10 @@ from pydantic import BaseModel, ConfigDict
"""
Azure AI Agent Provider Response Format Example
This sample demonstrates using AzureAIAgentsProvider with default_options
containing response_format for structured outputs.
This sample demonstrates using AzureAIAgentsProvider with response_format
for structured outputs in two ways:
1. Setting default response_format at agent creation time (default_options)
2. Overriding response_format at runtime (options parameter in agent.run)
"""
@@ -24,31 +26,57 @@ class WeatherInfo(BaseModel):
model_config = ConfigDict(extra="forbid")
class CityInfo(BaseModel):
"""Structured city information."""
city_name: str
population: int
country: str
model_config = ConfigDict(extra="forbid")
async def main() -> None:
"""Example of using default_options with response_format in AzureAIAgentsProvider."""
"""Example of using response_format at creation time and runtime."""
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
# Create agent with default response_format (WeatherInfo)
agent = await provider.create_agent(
name="WeatherReporter",
instructions="You provide weather reports in structured JSON format.",
name="StructuredReporter",
instructions="Return structured JSON based on the requested format.",
default_options={"response_format": WeatherInfo},
)
query = "What's the weather like in Paris today?"
print(f"User: {query}")
# Request 1: Uses default response_format from agent creation
print("--- Request 1: Using default response_format (WeatherInfo) ---")
query1 = "What's the weather like in Paris today?"
print(f"User: {query1}")
result = await agent.run(query)
result1 = await agent.run(query1)
if isinstance(result.value, WeatherInfo):
weather = result.value
if isinstance(result1.value, WeatherInfo):
weather = result1.value
print("Agent:")
print(f"Location: {weather.location}")
print(f"Temperature: {weather.temperature}")
print(f"Conditions: {weather.conditions}")
print(f"Recommendation: {weather.recommendation}")
print(f" Location: {weather.location}")
print(f" Temperature: {weather.temperature}")
print(f" Conditions: {weather.conditions}")
print(f" Recommendation: {weather.recommendation}")
# Request 2: Override response_format at runtime with CityInfo
print("\n--- Request 2: Runtime override with CityInfo ---")
query2 = "Tell me about Tokyo."
print(f"User: {query2}")
result2 = await agent.run(query2, options={"response_format": CityInfo})
if isinstance(result2.value, CityInfo):
city = result2.value
print("Agent:")
print(f" City: {city.city_name}")
print(f" Population: {city.population}")
print(f" Country: {city.country}")
if __name__ == "__main__":
@@ -10,8 +10,10 @@ from pydantic import BaseModel, ConfigDict
"""
OpenAI Assistant Provider Response Format Example
This sample demonstrates using OpenAIAssistantProvider with default_options
containing response_format for structured outputs.
This sample demonstrates using OpenAIAssistantProvider with response_format
for structured outputs in two ways:
1. Setting default response_format at agent creation time (default_options)
2. Overriding response_format at runtime (options parameter in agent.run)
"""
@@ -25,33 +27,59 @@ class WeatherInfo(BaseModel):
model_config = ConfigDict(extra="forbid")
class CityInfo(BaseModel):
"""Structured city information."""
city_name: str
population: int
country: str
model_config = ConfigDict(extra="forbid")
async def main() -> None:
"""Example of using default_options with response_format in OpenAIAssistantProvider."""
"""Example of using response_format at creation time and runtime."""
async with (
AsyncOpenAI() as client,
OpenAIAssistantProvider(client) as provider,
):
# Create agent with default response_format (WeatherInfo)
agent = await provider.create_agent(
name="WeatherReporter",
name="StructuredReporter",
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
instructions="You provide weather reports in structured JSON format.",
instructions="Return structured JSON based on the requested format.",
default_options={"response_format": WeatherInfo},
)
try:
query = "What's the weather like in Paris today?"
print(f"User: {query}")
# Request 1: Uses default response_format from agent creation
print("--- Request 1: Using default response_format (WeatherInfo) ---")
query1 = "What's the weather like in Paris today?"
print(f"User: {query1}")
result = await agent.run(query)
result1 = await agent.run(query1)
if isinstance(result.value, WeatherInfo):
weather = result.value
if isinstance(result1.value, WeatherInfo):
weather = result1.value
print("Agent:")
print(f" Location: {weather.location}")
print(f" Temperature: {weather.temperature}")
print(f" Conditions: {weather.conditions}")
print(f" Recommendation: {weather.recommendation}")
# Request 2: Override response_format at runtime with CityInfo
print("\n--- Request 2: Runtime override with CityInfo ---")
query2 = "Tell me about Tokyo."
print(f"User: {query2}")
result2 = await agent.run(query2, options={"response_format": CityInfo})
if isinstance(result2.value, CityInfo):
city = result2.value
print("Agent:")
print(f" City: {city.city_name}")
print(f" Population: {city.population}")
print(f" Country: {city.country}")
finally:
await client.beta.assistants.delete(agent.id)