Skip to content

Commit 5f694c9

Browse files
committed
fix type errors in FileSearchTool integration tests
- Add type assertions for store.name (cannot be None) - Initialize file/vector_store/store to None before try blocks - Update cleanup checks from 'in locals()' to 'is not None' - Simplify streaming tests to use stream_text() instead of raw events - All pyright type checks now pass on changed files
1 parent bc278e8 commit 5f694c9

File tree

2 files changed

+72
-63
lines changed

2 files changed

+72
-63
lines changed

tests/models/test_google.py

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,38 +3209,39 @@ async def test_google_model_file_search_tool(allow_model_requests: None, google_
32093209
from pydantic_ai.builtin_tools import FileSearchTool
32103210

32113211
client = google_provider.client
3212-
3212+
32133213
import tempfile
3214+
32143215
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
32153216
f.write('Paris is the capital of France. The Eiffel Tower is a famous landmark in Paris.')
32163217
test_file_path = f.name
3217-
3218+
3219+
store = None
32183220
try:
3219-
store = await client.aio.file_search_stores.create(
3220-
config={'display_name': 'test-file-search-store'}
3221-
)
3222-
3221+
store = await client.aio.file_search_stores.create(config={'display_name': 'test-file-search-store'})
3222+
assert store.name is not None
3223+
32233224
with open(test_file_path, 'rb') as f:
32243225
await client.aio.file_search_stores.upload_to_file_search_store(
3225-
file_search_store_name=store.name,
3226-
file=f,
3227-
config={'mime_type': 'text/plain'}
3226+
file_search_store_name=store.name, file=f, config={'mime_type': 'text/plain'}
32283227
)
3229-
3228+
32303229
import asyncio
3230+
32313231
await asyncio.sleep(3)
3232-
3232+
32333233
model = GoogleModel('gemini-2.5-pro', provider=google_provider)
32343234
agent = Agent(model=model, builtin_tools=[FileSearchTool(vector_store_ids=[store.name])])
3235-
3235+
32363236
result = await agent.run('What is the capital of France according to my files?')
3237-
3237+
32383238
assert 'Paris' in result.output or 'paris' in result.output.lower()
3239-
3239+
32403240
finally:
32413241
import os
3242+
32423243
os.unlink(test_file_path)
3243-
if 'store' in locals():
3244+
if store is not None and store.name is not None:
32443245
await client.aio.file_search_stores.delete(name=store.name, config={'force': True})
32453246

32463247

@@ -3250,46 +3251,44 @@ async def test_google_model_file_search_tool_stream(allow_model_requests: None,
32503251
from pydantic_ai.builtin_tools import FileSearchTool
32513252

32523253
client = google_provider.client
3253-
3254+
32543255
import tempfile
3256+
32553257
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
32563258
f.write('The Louvre Museum is located in Paris, France. It houses the Mona Lisa.')
32573259
test_file_path = f.name
3258-
3260+
3261+
store = None
32593262
try:
3260-
store = await client.aio.file_search_stores.create(
3261-
config={'display_name': 'test-file-search-stream'}
3262-
)
3263-
3263+
store = await client.aio.file_search_stores.create(config={'display_name': 'test-file-search-stream'})
3264+
assert store.name is not None
3265+
32643266
with open(test_file_path, 'rb') as f:
32653267
await client.aio.file_search_stores.upload_to_file_search_store(
3266-
file_search_store_name=store.name,
3267-
file=f,
3268-
config={'mime_type': 'text/plain'}
3268+
file_search_store_name=store.name, file=f, config={'mime_type': 'text/plain'}
32693269
)
3270-
3270+
32713271
import asyncio
3272+
32723273
await asyncio.sleep(3)
3273-
3274+
32743275
model = GoogleModel('gemini-2.5-pro', provider=google_provider)
32753276
agent = Agent(model=model, builtin_tools=[FileSearchTool(vector_store_ids=[store.name])])
3276-
3277-
events = []
3278-
async with agent.iter(user_prompt='Where is the Louvre Museum according to my files?') as agent_run:
3279-
async for node in agent_run:
3280-
if Agent.is_model_request_node(node):
3281-
async with node.stream(agent_run.ctx) as stream:
3282-
async for event in stream:
3283-
events.append(event)
3284-
3285-
assert len(events) > 0
3286-
output = ''.join(str(e.part.content) if hasattr(e, 'part') and hasattr(e.part, 'content') else '' for e in events)
3277+
3278+
result_text: list[str] = []
3279+
async with agent.run_stream('Where is the Louvre Museum according to my files?') as result:
3280+
async for text in result.stream_text(delta=False):
3281+
result_text.append(text)
3282+
output = await result.get_output()
3283+
3284+
assert len(result_text) > 0
32873285
assert 'Paris' in output or 'France' in output or 'Louvre' in output or 'paris' in output.lower()
3288-
3286+
32893287
finally:
32903288
import os
3289+
32913290
os.unlink(test_file_path)
3292-
if 'store' in locals():
3291+
if store is not None and store.name is not None:
32933292
await client.aio.file_search_stores.delete(name=store.name, config={'force': True})
32943293

32953294

tests/models/test_openai_responses.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7445,35 +7445,40 @@ async def test_openai_responses_model_file_search_tool(allow_model_requests: Non
74457445
from pydantic_ai.providers.openai import OpenAIProvider
74467446

74477447
async_client = AsyncOpenAI(api_key=openai_api_key)
7448-
7448+
74497449
import tempfile
7450+
74507451
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
74517452
f.write('Paris is the capital of France. It is known for the Eiffel Tower.')
74527453
test_file_path = f.name
7453-
7454+
7455+
file = None
7456+
vector_store = None
74547457
try:
74557458
with open(test_file_path, 'rb') as f:
74567459
file = await async_client.files.create(file=f, purpose='assistants')
7457-
7460+
74587461
vector_store = await async_client.vector_stores.create(name='test-file-search')
74597462
await async_client.vector_stores.files.create(vector_store_id=vector_store.id, file_id=file.id)
7460-
7463+
74617464
import asyncio
7465+
74627466
await asyncio.sleep(2)
7463-
7467+
74647468
model = OpenAIResponsesModel('gpt-4o', provider=OpenAIProvider(openai_client=async_client))
74657469
agent = Agent(model=model, builtin_tools=[FileSearchTool(vector_store_ids=[vector_store.id])])
7466-
7470+
74677471
result = await agent.run('What is the capital of France according to my files?')
7468-
7472+
74697473
assert 'Paris' in result.output or 'paris' in result.output.lower()
7470-
7474+
74717475
finally:
74727476
import os
7477+
74737478
os.unlink(test_file_path)
7474-
if 'file' in locals():
7479+
if file is not None:
74757480
await async_client.files.delete(file.id)
7476-
if 'vector_store' in locals():
7481+
if vector_store is not None:
74777482
await async_client.vector_stores.delete(vector_store.id)
74787483
await async_client.close()
74797484

@@ -7487,40 +7492,45 @@ async def test_openai_responses_model_file_search_tool_stream(allow_model_reques
74877492
from pydantic_ai.providers.openai import OpenAIProvider
74887493

74897494
async_client = AsyncOpenAI(api_key=openai_api_key)
7490-
7495+
74917496
import tempfile
7497+
74927498
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
74937499
f.write('The Eiffel Tower is located in Paris, France.')
74947500
test_file_path = f.name
7495-
7501+
7502+
file = None
7503+
vector_store = None
74967504
try:
74977505
with open(test_file_path, 'rb') as f:
74987506
file = await async_client.files.create(file=f, purpose='assistants')
7499-
7507+
75007508
vector_store = await async_client.vector_stores.create(name='test-file-search-stream')
75017509
await async_client.vector_stores.files.create(vector_store_id=vector_store.id, file_id=file.id)
7502-
7510+
75037511
import asyncio
7512+
75047513
await asyncio.sleep(2)
7505-
7514+
75067515
model = OpenAIResponsesModel('gpt-4o', provider=OpenAIProvider(openai_client=async_client))
75077516
agent = Agent(model=model, builtin_tools=[FileSearchTool(vector_store_ids=[vector_store.id])])
7508-
7509-
parts = []
7517+
7518+
result_text: list[str] = []
75107519
async with agent.run_stream('Where is the Eiffel Tower according to my files?') as result:
7511-
async for part in result.stream_responses(debounce_by=None):
7512-
parts.append(part)
7520+
async for text in result.stream_text(delta=False):
7521+
result_text.append(text)
75137522
output = await result.get_output()
7514-
7515-
assert len(parts) > 0
7523+
7524+
assert len(result_text) > 0
75167525
assert 'Paris' in output or 'France' in output or 'paris' in output.lower()
7517-
7526+
75187527
finally:
75197528
import os
7529+
75207530
os.unlink(test_file_path)
7521-
if 'file' in locals():
7531+
if file is not None:
75227532
await async_client.files.delete(file.id)
7523-
if 'vector_store' in locals():
7533+
if vector_store is not None:
75247534
await async_client.vector_stores.delete(vector_store.id)
75257535
await async_client.close()
75267536

0 commit comments

Comments
 (0)