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