Skip to content

Commit 65cb91c

Browse files
authored
Add input validation and type conversion for user input (#1462)
## Summary Fixed a type safety issue where user input was being used as a string without conversion to integer, which could cause runtime errors and type mismatches. ## Problem The code was using `input()` which returns a string, but then using it directly in the f-string without converting it to an integer. This could cause: - Type mismatches when the string is passed to functions expecting integers - Runtime errors when users enter non-numeric input - Inconsistent behavior with the function signature expectations ## Changes Made Added proper input validation and type conversion: - Wrapped the input processing in a try-except block - Convert user input to integer using `int(user_input)` - Added error handling for invalid input with user-friendly message - Used the converted integer value in the f-string instead of raw string input This ensures type safety and provides better user experience with proper error handling.
1 parent e4699c3 commit 65cb91c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

examples/basic/agent_lifecycle_example.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,15 @@ class FinalResult(BaseModel):
8484

8585
async def main() -> None:
8686
user_input = input("Enter a max number: ")
87-
await Runner.run(
88-
start_agent,
89-
input=f"Generate a random number between 0 and {user_input}.",
90-
)
87+
try:
88+
max_number = int(user_input)
89+
await Runner.run(
90+
start_agent,
91+
input=f"Generate a random number between 0 and {max_number}.",
92+
)
93+
except ValueError:
94+
print("Please enter a valid integer.")
95+
return
9196

9297
print("Done!")
9398

0 commit comments

Comments
 (0)