Skip to content

Commit 3ee3ca1

Browse files
committed
Waste fewer tokens on Mistral fake assistant message
1 parent 8405843 commit 3ee3ca1

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

pydantic_ai_slim/pydantic_ai/models/mistral.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,7 @@ def _map_messages(self, messages: list[ModelMessage]) -> list[MistralMessages]:
494494
next_message = mistral_messages[i + 1]
495495
if isinstance(next_message, MistralUserMessage):
496496
# Insert a dummy assistant message
497-
processed_messages.append(
498-
MistralAssistantMessage(content=[MistralTextChunk(text="Thanks, I'll look at file.")])
499-
)
497+
processed_messages.append(MistralAssistantMessage(content=[MistralTextChunk(text='OK')]))
500498

501499
return processed_messages
502500

tests/models/cassettes/test_mistral/test_image_as_binary_content_tool_response.yaml

+20-20
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ interactions:
5454
ratelimitbysize-remaining:
5555
- '4968000'
5656
ratelimitbysize-reset:
57-
- '37'
57+
- '19'
5858
transfer-encoding:
5959
- chunked
6060
parsed_body:
@@ -69,10 +69,10 @@ interactions:
6969
- function:
7070
arguments: '{}'
7171
name: get_image
72-
id: u73r99WZa
72+
id: 7y9B4SNod
7373
index: 0
74-
created: 1745002643
75-
id: 2dfb1f83295645ec8a175ebe907cfe45
74+
created: 1745623781
75+
id: fd8566b89bda45b2bdd0f70ee8251fa9
7676
model: pixtral-12b-latest
7777
object: chat.completion
7878
usage:
@@ -91,11 +91,12 @@ interactions:
9191
connection:
9292
- keep-alive
9393
content-length:
94-
- '2780323'
94+
- '2780299'
9595
content-type:
9696
- application/json
9797
cookie:
98-
- __cf_bm=vUohgAc2GQ6CS3oUu0wqKHSQ4Bb8zlsU_sUy_WxC4lM-1745002643-1.0.1.1-DfmXYGY6.TDqs0NU1yWv41YsuuULmCj_PqmoicCRbV1tR_7wRxdFD2RiXjuUqjdb0DLhQ9ZllWwqwGx7M3aVUUUz1GaEBPzXO_A1y9guOX0
98+
- __cf_bm=L9eUr5YJZt1oi_FgR0aJp8aW9lFNKRj70b6L2xULOaM-1745623781-1.0.1.1-HZH8890qyux.GUDgiWL3BwtU6aZ7b59uO29HFhwr7je6zNd61nUVFD7OVLEqL8Ae4ccNsB5XOVGNHRzrxZ43gpp2BVQ149ql1H_LQfomy5M;
99+
_cfuvid=92xu1F_Dht_xLrPF.goEaIF6kjjswk5T6qkdnFZX3QI-1745623781518-0.0.1.1-604800000
99100
host:
100101
- api.mistral.ai
101102
method: POST
@@ -112,14 +113,14 @@ interactions:
112113
- function:
113114
arguments: '{}'
114115
name: get_image
115-
id: u73r99WZa
116+
id: 7y9B4SNod
116117
index: 0
117118
type: function
118119
- content: See file 1.
119120
role: tool
120-
tool_call_id: u73r99WZa
121+
tool_call_id: 7y9B4SNod
121122
- content:
122-
- text: Thanks, I'll look at file.
123+
- text: OK
123124
type: text
124125
prefix: false
125126
role: assistant
@@ -154,17 +155,17 @@ interactions:
154155
connection:
155156
- keep-alive
156157
content-length:
157-
- '566'
158+
- '509'
158159
content-type:
159160
- application/json
160161
ratelimitbysize-limit:
161162
- '5000000'
162163
ratelimitbysize-query-cost:
163164
- '32004'
164165
ratelimitbysize-remaining:
165-
- '4935996'
166+
- '4844519'
166167
ratelimitbysize-reset:
167-
- '37'
168+
- '19'
168169
transfer-encoding:
169170
- chunked
170171
parsed_body:
@@ -173,19 +174,18 @@ interactions:
173174
index: 0
174175
logprobs: null
175176
message:
176-
content: The image shows a halved kiwi fruit. Kiwis are known for their bright green flesh with tiny black seeds
177-
and a sweet, tangy flavor. They are also rich in vitamin C and fiber. Is there anything specific you would like
178-
to know about kiwis or the image?
177+
content: The image shows a kiwi fruit that has been cut in half. Kiwis are known for their bright green flesh with
178+
tiny black seeds and a sweet, tangy flavor. They are also rich in vitamin C and fiber.
179179
role: assistant
180180
tool_calls: null
181-
created: 1745002645
182-
id: b0b1416fa1014119a29a5ad4d1121112
181+
created: 1745623783
182+
id: 27506ccefb4949d1a9ef5b4ab13549db
183183
model: pixtral-12b-latest
184184
object: chat.completion
185185
usage:
186-
completion_tokens: 60
187-
prompt_tokens: 2930
188-
total_tokens: 2990
186+
completion_tokens: 47
187+
prompt_tokens: 2921
188+
total_tokens: 2968
189189
status:
190190
code: 200
191191
message: OK

tests/models/test_mistral.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ async def get_image() -> BinaryContent:
17611761
]
17621762
),
17631763
ModelResponse(
1764-
parts=[ToolCallPart(tool_name='get_image', args='{}', tool_call_id='u73r99WZa')],
1764+
parts=[ToolCallPart(tool_name='get_image', args='{}', tool_call_id='7y9B4SNod')],
17651765
model_name='pixtral-12b-latest',
17661766
timestamp=IsDatetime(),
17671767
),
@@ -1770,7 +1770,7 @@ async def get_image() -> BinaryContent:
17701770
ToolReturnPart(
17711771
tool_name='get_image',
17721772
content='See file 1.',
1773-
tool_call_id='u73r99WZa',
1773+
tool_call_id='7y9B4SNod',
17741774
timestamp=IsDatetime(),
17751775
),
17761776
UserPromptPart(
@@ -1785,7 +1785,7 @@ async def get_image() -> BinaryContent:
17851785
ModelResponse(
17861786
parts=[
17871787
TextPart(
1788-
content='The image shows a halved kiwi fruit. Kiwis are known for their bright green flesh with tiny black seeds and a sweet, tangy flavor. They are also rich in vitamin C and fiber. Is there anything specific you would like to know about kiwis or the image?'
1788+
content='The image shows a kiwi fruit that has been cut in half. Kiwis are known for their bright green flesh with tiny black seeds and a sweet, tangy flavor. They are also rich in vitamin C and fiber.'
17891789
)
17901790
],
17911791
model_name='pixtral-12b-latest',

0 commit comments

Comments
 (0)