From cd194bc918fb995deb5224bcd1ba3cd28171221c Mon Sep 17 00:00:00 2001 From: Abbas Asad <168946441+Abbas-Asad@users.noreply.github.com> Date: Thu, 14 Aug 2025 21:23:06 +0500 Subject: [PATCH] Add input validation and type conversion for user input (in lifecycle_example.py) ## 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. --- examples/basic/lifecycle_example.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/basic/lifecycle_example.py b/examples/basic/lifecycle_example.py index 02ce449f4..c31ec5e12 100644 --- a/examples/basic/lifecycle_example.py +++ b/examples/basic/lifecycle_example.py @@ -88,11 +88,16 @@ class FinalResult(BaseModel): async def main() -> None: user_input = input("Enter a max number: ") - await Runner.run( - start_agent, - hooks=hooks, - input=f"Generate a random number between 0 and {user_input}.", - ) + try: + max_number = int(user_input) + await Runner.run( + start_agent, + hooks=hooks, + input=f"Generate a random number between 0 and {max_number}.", + ) + except ValueError: + print("Please enter a valid integer.") + return print("Done!")