Skip to content

Commit 97ff651

Browse files
committed
more
1 parent 6bcc1a8 commit 97ff651

File tree

4 files changed

+100
-12
lines changed

4 files changed

+100
-12
lines changed

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
ModelResponsePart,
2727
ModelResponseStreamEvent,
2828
RetryPromptPart,
29+
ServerToolCallPart,
30+
ServerToolReturnPart,
2931
SystemPromptPart,
3032
TextPart,
3133
ToolCallPart,
@@ -466,7 +468,17 @@ def _process_response_from_parts(
466468
) -> ModelResponse:
467469
items: list[ModelResponsePart] = []
468470
for part in parts:
469-
if part.text:
471+
if part.executable_code is not None:
472+
items.append(ServerToolCallPart(args=part.executable_code.model_dump(), tool_name='code_execution'))
473+
elif part.code_execution_result is not None:
474+
items.append(
475+
ServerToolReturnPart(
476+
tool_name='code_execution',
477+
content=part.code_execution_result.output,
478+
tool_call_id="It doesn't have.",
479+
)
480+
)
481+
elif part.text:
470482
items.append(TextPart(content=part.text))
471483
elif part.function_call:
472484
assert part.function_call.name is not None

tests/models/cassettes/test_google/test_google_model_code_execution_tool.yaml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ interactions:
3232
alt-svc:
3333
- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
3434
content-length:
35-
- '824'
35+
- '1462'
3636
content-type:
3737
- application/json; charset=UTF-8
3838
server-timing:
39-
- gfet4t7; dur=634
39+
- gfet4t7; dur=7674
4040
transfer-encoding:
4141
- chunked
4242
vary:
@@ -47,24 +47,42 @@ interactions:
4747
candidates:
4848
- content:
4949
parts:
50+
- text: |+
51+
To determine the current day in Utrecht, I need to know the current date and time. I will use a tool to get this information.
52+
53+
- executableCode:
54+
code: |
55+
import datetime
56+
import pytz
57+
58+
utrecht_timezone = pytz.timezone('Europe/Amsterdam')
59+
now_utrecht = datetime.datetime.now(utrecht_timezone)
60+
print(now_utrecht.strftime("%A, %Y-%m-%d"))
61+
language: PYTHON
62+
- codeExecutionResult:
63+
outcome: OUTCOME_OK
64+
output: |
65+
Wednesday, 2025-05-28
5066
- text: |
51-
To give you the exact day in Utrecht, I need to know the current date. Can you please provide the date? I can then determine the day.
67+
Today is Wednesday, May 28, 2025 in Utrecht.
5268
role: model
5369
finishReason: STOP
5470
modelVersion: gemini-2.0-flash
55-
responseId: WN42aLH_CoKZgLUP97W1wQg
71+
responseId: 8ww3aLDxJY24qsMP97vYeA
5672
usageMetadata:
57-
candidatesTokenCount: 32
73+
candidatesTokenCount: 119
5874
candidatesTokensDetails:
5975
- modality: TEXT
60-
tokenCount: 32
76+
tokenCount: 119
6177
promptTokenCount: 13
6278
promptTokensDetails:
6379
- modality: TEXT
6480
tokenCount: 13
81+
toolUsePromptTokenCount: 114
6582
toolUsePromptTokensDetails:
6683
- modality: TEXT
67-
totalTokenCount: 45
84+
tokenCount: 114
85+
totalTokenCount: 246
6886
status:
6987
code: 200
7088
message: OK

tests/models/test_google.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
PartDeltaEvent,
2828
PartStartEvent,
2929
RetryPromptPart,
30+
ServerToolCallPart,
31+
ServerToolReturnPart,
3032
SystemPromptPart,
3133
TextPart,
3234
TextPartDelta,
@@ -41,7 +43,7 @@
4143

4244
with try_import() as imports_successful:
4345
from google.genai import _api_client
44-
from google.genai.types import HarmBlockThreshold, HarmCategory
46+
from google.genai.types import HarmBlockThreshold, HarmCategory, Language
4547

4648
from pydantic_ai.models.google import GoogleModel, GoogleModelSettings
4749
from pydantic_ai.providers.google import GoogleProvider
@@ -597,4 +599,60 @@ async def test_google_model_code_execution_tool(allow_model_requests: None, goog
597599
agent = Agent(m, system_prompt='You are a helpful chatbot.', builtin_tools=[CodeExecutionTool()])
598600

599601
result = await agent.run('What day is today in Utrecht?')
600-
assert result.output == snapshot('Today is Wednesday, May 28, 2025, in Utrecht.\n')
602+
assert result.all_messages() == snapshot(
603+
[
604+
ModelRequest(
605+
parts=[
606+
SystemPromptPart(content='You are a helpful chatbot.', timestamp=IsDatetime()),
607+
UserPromptPart(content='What day is today in Utrecht?', timestamp=IsDatetime()),
608+
]
609+
),
610+
ModelResponse(
611+
parts=[
612+
TextPart(
613+
content="""\
614+
To determine the current day in Utrecht, I need to know the current date and time. I will use a tool to get this information.
615+
616+
"""
617+
),
618+
ServerToolCallPart(
619+
tool_name='code_execution',
620+
args={
621+
'code': """\
622+
import datetime
623+
import pytz
624+
625+
utrecht_timezone = pytz.timezone('Europe/Amsterdam')
626+
now_utrecht = datetime.datetime.now(utrecht_timezone)
627+
print(now_utrecht.strftime("%A, %Y-%m-%d"))
628+
""",
629+
'language': Language.PYTHON,
630+
},
631+
tool_call_id='pyd_ai_8cc0806969e94245b2877a60fab8bf7e',
632+
),
633+
ServerToolReturnPart(
634+
tool_name='code_execution',
635+
content='Wednesday, 2025-05-28\n',
636+
tool_call_id="It doesn't have.",
637+
timestamp=IsDatetime(),
638+
),
639+
TextPart(content='Today is Wednesday, May 28, 2025 in Utrecht.\n'),
640+
],
641+
usage=Usage(
642+
requests=1,
643+
request_tokens=13,
644+
response_tokens=119,
645+
total_tokens=246,
646+
details={
647+
'tool_use_prompt_tokens': 114,
648+
'text_candidates_tokens': 119,
649+
'text_prompt_tokens': 13,
650+
'text_tool_use_prompt_tokens': 114,
651+
},
652+
),
653+
model_name='gemini-2.0-flash',
654+
timestamp=IsDatetime(),
655+
vendor_details={'finish_reason': 'STOP'},
656+
),
657+
]
658+
)

tests/models/test_groq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ async def test_groq_model_web_search_tool(allow_model_requests: None, groq_api_k
702702
ServerToolCallPart(
703703
tool_name='search',
704704
args='{"query": "What is the current date?"}',
705-
tool_call_id='0',
705+
tool_call_id=IsStr(),
706706
model_name='groq',
707707
),
708708
ServerToolReturnPart(
@@ -784,7 +784,7 @@ async def test_groq_model_web_search_tool(allow_model_requests: None, groq_api_k
784784
Score: 0.2134
785785
786786
""",
787-
tool_call_id='0',
787+
tool_call_id=IsStr(),
788788
timestamp=IsDatetime(),
789789
),
790790
TextPart(content='The current day is Tuesday.'),

0 commit comments

Comments
 (0)