diff --git a/guardrails/validator_service/__init__.py b/guardrails/validator_service/__init__.py index 0bb2f8b89..7139603e0 100644 --- a/guardrails/validator_service/__init__.py +++ b/guardrails/validator_service/__init__.py @@ -29,6 +29,9 @@ def should_run_sync(): process_count = os.environ.get("GUARDRAILS_PROCESS_COUNT") + # Short-circuit for 'GUARDRAILS_RUN_SYNC', and avoid duplicate lower() & lookups + run_sync = os.environ.get("GUARDRAILS_RUN_SYNC", "false") + run_sync_lower = run_sync.lower() if process_count is not None: warnings.warn( "GUARDRAILS_PROCESS_COUNT is deprecated" @@ -37,13 +40,15 @@ def should_run_sync(): DeprecationWarning, ) process_count = int(process_count) - run_sync = os.environ.get("GUARDRAILS_RUN_SYNC", "false") - bool_values = ["true", "false"] - if run_sync.lower() not in bool_values: + # If process_count == 1, immediately return True for efficiency + if process_count == 1: + return True + # Avoid reconstructing list for each call + if run_sync_lower not in ("true", "false"): warnings.warn( - f"GUARDRAILS_RUN_SYNC must be one of {bool_values}! Defaulting to 'false'." + "GUARDRAILS_RUN_SYNC must be one of ['true', 'false']! Defaulting to 'false'." ) - return process_count == 1 or run_sync.lower() == "true" + return run_sync_lower == "true" def get_loop() -> asyncio.AbstractEventLoop: