Commit b8f73ce
committed
Fix PydanticAIRunner to use proper Pydantic AI message capture API
CRITICAL FIX: The original implementation incorrectly assumed that
UsageLimitExceeded would have a message_history attribute, but this
is NOT how Pydantic AI works.
## What Was Wrong
The initial implementation tried to extract message history directly from
the UsageLimitExceeded exception using a non-existent attribute:
```python
def _extract_messages(e: UsageLimitExceeded) -> list[ModelMessage]:
if hasattr(e, "message_history") and e.message_history:
return list(e.message_history)
```
This approach was based on the OpenAI Agents SDK pattern (MaxTurnsExceeded.run_data),
but Pydantic AI uses a completely different pattern.
## How Pydantic AI Actually Works
Per Pydantic AI documentation (https://ai.pydantic.dev/agents/#model-errors):
- Use `capture_run_messages()` context manager to capture messages
- The context manager populates a list during agent.run() execution
- If an exception occurs, the captured messages are still available
- This is the official, documented approach
Reference: pydantic/pydantic-ai#1083
## Changes Made
1. **Updated imports**:
- Added `capture_run_messages` from pydantic_ai
- Removed obsolete `_extract_messages()` helper function
2. **Rewrote run() method**:
- Wrapped agent.run() in `with capture_run_messages() as messages:`
- On UsageLimitExceeded, use the captured messages for recovery
- Messages are properly populated by the context manager
3. **Rewrote run_streamed() method**:
- Same pattern as run() but for streaming
- Uses capture_run_messages() consistently
4. **Updated all recovery tests**:
- Mock capture_run_messages context manager properly
- Use `@patch("agentexec.runners.pydantic_ai.capture_run_messages")`
- Mock return value with `__enter__` to simulate context manager
## Verification
✅ All 21 PydanticAIRunner tests pass
✅ All 49 tests in test suite pass
✅ Recovery mechanism properly captures and reuses conversation history
✅ Implementation follows official Pydantic AI patterns
## Why This Matters
Without this fix, the recovery mechanism would NEVER work correctly because:
1. Message history would always be empty
2. Recovery would lose all conversation context
3. The wrap-up prompt would be sent without prior conversation
This fix ensures the PydanticAIRunner behaves correctly according to
Pydantic AI's actual API, not just our assumptions.1 parent d6ba449 commit b8f73ce
File tree
2 files changed
+98
-105
lines changed- src/agentexec/runners
- tests
2 files changed
+98
-105
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 22 | | |
41 | 23 | | |
42 | 24 | | |
| |||
135 | 117 | | |
136 | 118 | | |
137 | 119 | | |
138 | | - | |
| 120 | + | |
139 | 121 | | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
175 | 157 | | |
176 | 158 | | |
177 | 159 | | |
| |||
203 | 185 | | |
204 | 186 | | |
205 | 187 | | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
203 | | - | |
| 203 | + | |
| 204 | + | |
204 | 205 | | |
205 | 206 | | |
206 | 207 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
216 | | - | |
217 | | - | |
218 | | - | |
| 217 | + | |
| 218 | + | |
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
222 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
223 | 229 | | |
224 | 230 | | |
225 | 231 | | |
| |||
242 | 248 | | |
243 | 249 | | |
244 | 250 | | |
245 | | - | |
| 251 | + | |
246 | 252 | | |
247 | 253 | | |
248 | 254 | | |
249 | 255 | | |
250 | 256 | | |
251 | 257 | | |
252 | | - | |
| 258 | + | |
| 259 | + | |
253 | 260 | | |
254 | 261 | | |
255 | 262 | | |
| |||
260 | 267 | | |
261 | 268 | | |
262 | 269 | | |
263 | | - | |
264 | | - | |
| 270 | + | |
| 271 | + | |
265 | 272 | | |
266 | 273 | | |
267 | 274 | | |
268 | 275 | | |
269 | 276 | | |
270 | 277 | | |
271 | | - | |
272 | | - | |
| 278 | + | |
| 279 | + | |
273 | 280 | | |
| 281 | + | |
274 | 282 | | |
275 | 283 | | |
276 | 284 | | |
| |||
284 | 292 | | |
285 | 293 | | |
286 | 294 | | |
287 | | - | |
288 | | - | |
| 295 | + | |
| 296 | + | |
289 | 297 | | |
290 | 298 | | |
291 | 299 | | |
| |||
332 | 340 | | |
333 | 341 | | |
334 | 342 | | |
335 | | - | |
| 343 | + | |
| 344 | + | |
336 | 345 | | |
337 | 346 | | |
338 | 347 | | |
| |||
343 | 352 | | |
344 | 353 | | |
345 | 354 | | |
346 | | - | |
347 | | - | |
| 355 | + | |
| 356 | + | |
348 | 357 | | |
349 | 358 | | |
| 359 | + | |
350 | 360 | | |
| 361 | + | |
351 | 362 | | |
352 | 363 | | |
353 | 364 | | |
| |||
0 commit comments