Skip to content

Commit 1b87a07

Browse files
giles17Copilot
andauthored
Python: Fix sample bugs in file search and web search samples (#4049)
- Fix file search samples: return vector_store.id string instead of Content object to avoid JSON serialization error - Fix web search sample: use correct web_search_options parameter for ChatClient instead of ResponsesClient's user_location parameter - Fix assistants client: pass tool_resources from options to run_options so vector store IDs reach thread creation - Add error handling for cleanup in Azure file search sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c23bc13 commit 1b87a07

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

python/packages/core/agent_framework/openai/_assistants_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ def _prepare_options(
670670
tool_choice = options.get("tool_choice")
671671
tools = options.get("tools")
672672
response_format = options.get("response_format")
673+
tool_resources = options.get("tool_resources")
673674

674675
if max_tokens is not None:
675676
run_options["max_completion_tokens"] = max_tokens
@@ -683,6 +684,9 @@ def _prepare_options(
683684
if allow_multiple_tool_calls is not None:
684685
run_options["parallel_tool_calls"] = allow_multiple_tool_calls
685686

687+
if tool_resources is not None:
688+
run_options["tool_resources"] = tool_resources
689+
686690
tool_mode = validate_tool_mode(tool_choice)
687691
tool_definitions: list[MutableMapping[str, Any]] = []
688692
# Always include tools if provided, regardless of tool_choice

python/samples/02-agents/providers/azure_openai/azure_responses_client_with_file_search.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

33
import asyncio
4+
import contextlib
45

5-
from agent_framework import Agent, Content
6+
from agent_framework import Agent
67
from agent_framework.azure import AzureOpenAIResponsesClient
78
from azure.identity import AzureCliCredential
89

@@ -22,7 +23,7 @@
2223
# Helper functions
2324

2425

25-
async def create_vector_store(client: AzureOpenAIResponsesClient) -> tuple[str, Content]:
26+
async def create_vector_store(client: AzureOpenAIResponsesClient) -> tuple[str, str]:
2627
"""Create a vector store with sample documents."""
2728
file = await client.client.files.create(
2829
file=("todays_weather.txt", b"The weather today is sunny with a high of 75F."), purpose="assistants"
@@ -35,13 +36,15 @@ async def create_vector_store(client: AzureOpenAIResponsesClient) -> tuple[str,
3536
if result.last_error is not None:
3637
raise Exception(f"Vector store file processing failed with status: {result.last_error.message}")
3738

38-
return file.id, Content.from_hosted_vector_store(vector_store_id=vector_store.id)
39+
return file.id, vector_store.id
3940

4041

4142
async def delete_vector_store(client: AzureOpenAIResponsesClient, file_id: str, vector_store_id: str) -> None:
4243
"""Delete the vector store after using it."""
43-
await client.client.vector_stores.delete(vector_store_id=vector_store_id)
44-
await client.client.files.delete(file_id=file_id)
44+
with contextlib.suppress(Exception):
45+
await client.client.vector_stores.delete(vector_store_id=vector_store_id)
46+
with contextlib.suppress(Exception):
47+
await client.client.files.delete(file_id=file_id)
4548

4649

4750
async def main() -> None:

python/samples/02-agents/providers/openai/openai_chat_client_with_web_search.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ async def main() -> None:
1818

1919
# Create web search tool with location context
2020
web_search_tool = client.get_web_search_tool(
21-
user_location={"city": "Seattle", "country": "US"},
21+
web_search_options={
22+
"user_location": {
23+
"type": "approximate",
24+
"approximate": {"city": "Seattle", "country": "US"},
25+
},
26+
},
2227
)
2328

2429
agent = Agent(

python/samples/02-agents/providers/openai/openai_responses_client_with_file_search.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44

5-
from agent_framework import Agent, Content
5+
from agent_framework import Agent
66
from agent_framework.openai import OpenAIResponsesClient
77

88
"""
@@ -15,7 +15,7 @@
1515
# Helper functions
1616

1717

18-
async def create_vector_store(client: OpenAIResponsesClient) -> tuple[str, Content]:
18+
async def create_vector_store(client: OpenAIResponsesClient) -> tuple[str, str]:
1919
"""Create a vector store with sample documents."""
2020
file = await client.client.files.create(
2121
file=("todays_weather.txt", b"The weather today is sunny with a high of 75F."), purpose="user_data"
@@ -28,7 +28,7 @@ async def create_vector_store(client: OpenAIResponsesClient) -> tuple[str, Conte
2828
if result.last_error is not None:
2929
raise Exception(f"Vector store file processing failed with status: {result.last_error.message}")
3030

31-
return file.id, Content.from_hosted_vector_store(vector_store_id=vector_store.id)
31+
return file.id, vector_store.id
3232

3333

3434
async def delete_vector_store(client: OpenAIResponsesClient, file_id: str, vector_store_id: str) -> None:
@@ -53,14 +53,14 @@ async def main() -> None:
5353
)
5454

5555
if stream:
56-
print("Assistant: ", end="")
56+
print("Agent: ", end="")
5757
async for chunk in agent.run(message, stream=True):
5858
if chunk.text:
5959
print(chunk.text, end="")
6060
print("")
6161
else:
6262
response = await agent.run(message)
63-
print(f"Assistant: {response}")
63+
print(f"Agent: {response}")
6464
await delete_vector_store(client, file_id, vector_store_id)
6565

6666

0 commit comments

Comments
 (0)