Skip to content

Commit ddb7142

Browse files
committed
Raise ModelRetry when model/user supplies bad args
1 parent f3c5500 commit ddb7142

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/airline_agent/tools/booking.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import date, datetime, timedelta
33
from typing import Any
44

5+
from pydantic_ai import ModelRetry
56
from pydantic_ai.toolsets import FunctionToolset
67

78
from airline_agent.constants import DEMO_DATETIME
@@ -60,9 +61,9 @@ def search_flights(self, origin: str, destination: str, departure_date: str) ->
6061
"""
6162
try:
6263
dep = date.fromisoformat(departure_date)
63-
except Exception as e:
64+
except Exception: # noqa: BLE001
6465
msg = f"Invalid departure_date: {departure_date}"
65-
raise ValueError(msg) from e
66+
raise ModelRetry(msg) from None
6667

6768
return [
6869
fl
@@ -85,7 +86,7 @@ def get_fare_details(self, flight_id: str, fare_type: str = "basic") -> dict[str
8586
"""
8687
if flight_id not in self._flights:
8788
msg = f"Flight not found: {flight_id}"
88-
raise ValueError(msg)
89+
raise ModelRetry(msg)
8990

9091
flight = self._flights[flight_id]
9192

@@ -94,7 +95,7 @@ def get_fare_details(self, flight_id: str, fare_type: str = "basic") -> dict[str
9495
if not fare:
9596
available_fares = [f.fare_type for f in flight.fares]
9697
msg = f"Fare '{fare_type}' not available for flight {flight_id}. Available fares: {available_fares}"
97-
raise ValueError(msg)
98+
raise ModelRetry(msg)
9899

99100
return {
100101
"flight_id": flight_id,
@@ -132,7 +133,7 @@ def book_flights(
132133
"""
133134
if not flight_ids:
134135
msg = "At least one flight ID must be provided"
135-
raise ValueError(msg)
136+
raise ModelRetry(msg)
136137

137138
now = DEMO_DATETIME
138139
# Generate deterministic booking ID using seeded random
@@ -144,7 +145,7 @@ def book_flights(
144145
for flight_id in flight_ids:
145146
if flight_id not in self._flights:
146147
msg = f"Flight not found: {flight_id}"
147-
raise ValueError(msg)
148+
raise ModelRetry(msg)
148149

149150
flight = self._flights[flight_id]
150151

@@ -153,11 +154,11 @@ def book_flights(
153154
if not fare:
154155
available_fares = [f.fare_type for f in flight.fares]
155156
msg = f"Fare '{fare_type}' not available for flight {flight_id}. Available fares: {available_fares}"
156-
raise ValueError(msg)
157+
raise ModelRetry(msg)
157158

158159
if fare.seats_available <= 0:
159160
msg = f"No seats available for fare '{fare_type}' for flight {flight_id}"
160-
raise ValueError(msg)
161+
raise ModelRetry(msg)
161162

162163
flight_bookings.append(
163164
FlightBooking(
@@ -199,7 +200,7 @@ def get_booking(self, booking_id: str) -> Booking:
199200
"""
200201
if booking_id not in self._reservations:
201202
msg = f"Booking not found: {booking_id}"
202-
raise ValueError(msg)
203+
raise ModelRetry(msg)
203204
return self._reservations[booking_id]
204205

205206
def get_my_bookings(self) -> list[Booking]:
@@ -235,7 +236,7 @@ def add_service_to_booking(
235236
"""
236237
if booking_id not in self._reservations:
237238
msg = f"Booking not found: {booking_id}"
238-
raise ValueError(msg)
239+
raise ModelRetry(msg)
239240

240241
booking = self._reservations[booking_id]
241242

@@ -244,12 +245,12 @@ def add_service_to_booking(
244245
if not flight_booking:
245246
available_flights = [fb.flight_id for fb in booking.flights]
246247
msg = f"Flight {flight_id} not found in booking {booking_id}. Available flights: {available_flights}"
247-
raise ValueError(msg)
248+
raise ModelRetry(msg)
248249

249250
# Get the flight to check available add-ons
250251
if flight_id not in self._flights:
251252
msg = f"Flight not found: {flight_id}"
252-
raise ValueError(msg)
253+
raise ModelRetry(msg)
253254

254255
flight = self._flights[flight_id]
255256

@@ -260,7 +261,7 @@ def add_service_to_booking(
260261
msg = (
261262
f"Service '{service_type}' not available for flight {flight_id}. Available add-ons: {available_addons}"
262263
)
263-
raise ValueError(msg)
264+
raise ModelRetry(msg)
264265

265266
# Check if service is already included in the fare
266267
# Special handling for checked_bag (tracked via count, not in included_services)
@@ -270,19 +271,19 @@ def add_service_to_booking(
270271
f"Checked bag(s) are already included in the {flight_booking.fare_type} fare "
271272
f"for flight {flight_id} ({flight_booking.checked_bags_included} bag(s) included)"
272273
)
273-
raise ValueError(msg)
274+
raise ModelRetry(msg)
274275
elif service_type in flight_booking.included_services:
275276
msg = (
276277
f"Service '{service_type}' is already included in the {flight_booking.fare_type} fare "
277278
f"for flight {flight_id}"
278279
)
279-
raise ValueError(msg)
280+
raise ModelRetry(msg)
280281

281282
# Check if add-on already exists
282283
existing_addon = next((ao for ao in flight_booking.add_ons if ao.service_type == service_type), None)
283284
if existing_addon:
284285
msg = f"Service '{service_type}' has already been added to flight {flight_id} in this booking"
285-
raise ValueError(msg)
286+
raise ModelRetry(msg)
286287

287288
# Add the service add-on
288289
now = DEMO_DATETIME
@@ -313,7 +314,7 @@ def add_service_to_booking(
313314
# For non-seat services, validate that seat parameters weren't provided
314315
if seat_preference or seat_assignment:
315316
msg = "seat_preference and seat_assignment can only be set for seat selection service types"
316-
raise ValueError(msg)
317+
raise ModelRetry(msg)
317318

318319
addon = GenericServiceAddOn(
319320
service_type=service_type,
@@ -434,28 +435,28 @@ def check_in(self, booking_id: str, flight_id: str) -> Booking:
434435
"""
435436
if booking_id not in self._reservations:
436437
msg = f"Booking not found: {booking_id}"
437-
raise ValueError(msg)
438+
raise ModelRetry(msg)
438439

439440
booking = self._reservations[booking_id]
440441
if booking.status.status != "confirmed":
441442
msg = f"Cannot check in for booking {booking_id}: booking status is {booking.status.status}"
442-
raise ValueError(msg)
443+
raise ModelRetry(msg)
443444

444445
# Find the flight in the booking
445446
flight_booking = next((fb for fb in booking.flights if fb.flight_id == flight_id), None)
446447
if not flight_booking:
447448
available_flights = [fb.flight_id for fb in booking.flights]
448449
msg = f"Flight {flight_id} not found in booking {booking_id}. Available flights: {available_flights}"
449-
raise ValueError(msg)
450+
raise ModelRetry(msg)
450451

451452
if flight_booking.checked_in:
452453
msg = f"Already checked in for flight {flight_id} in booking {booking_id}"
453-
raise ValueError(msg)
454+
raise ModelRetry(msg)
454455

455456
# Get the flight details
456457
if flight_id not in self._flights:
457458
msg = f"Flight not found: {flight_id}"
458-
raise ValueError(msg)
459+
raise ModelRetry(msg)
459460

460461
flight = self._flights[flight_id]
461462
now = DEMO_DATETIME
@@ -491,7 +492,7 @@ def get_flight_timings(self, flight_id: str) -> dict[str, Any]:
491492
"""
492493
if flight_id not in self._flights:
493494
msg = f"Flight not found: {flight_id}"
494-
raise ValueError(msg)
495+
raise ModelRetry(msg)
495496

496497
flight = self._flights[flight_id]
497498
timings = self._calculate_check_in_timings(flight.departure)
@@ -531,7 +532,7 @@ def get_flight_status(self, flight_id: str) -> dict[str, Any]:
531532
"""
532533
if flight_id not in self._flights:
533534
msg = f"Flight not found: {flight_id}"
534-
raise ValueError(msg)
535+
raise ModelRetry(msg)
535536

536537
flight = self._flights[flight_id]
537538

0 commit comments

Comments
 (0)