From 4d700d6da356bf65b7e3bae3715801fe727a9cb0 Mon Sep 17 00:00:00 2001 From: Apurva Patil <161266535+ApurvaPatil2401@users.noreply.github.com> Date: Fri, 20 Feb 2026 06:58:29 +0000 Subject: [PATCH] feat(openmemory): add environment validation on startup --- contributing/samples/open_memory/main.py | 27 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/contributing/samples/open_memory/main.py b/contributing/samples/open_memory/main.py index e9eeb4a..b4e6a53 100644 --- a/contributing/samples/open_memory/main.py +++ b/contributing/samples/open_memory/main.py @@ -26,22 +26,37 @@ # Load environment variables from .env file if it exists load_dotenv() +def validate_environment(): + """Ensures all required API keys are present before starting.""" + missing_vars = [] + if not os.getenv('OPENMEMORY_API_KEY'): + missing_vars.append('OPENMEMORY_API_KEY') + if not os.getenv('GOOGLE_API_KEY'): + missing_vars.append('GOOGLE_API_KEY') + + if missing_vars: + error_msg = f"Missing required environment variables: {', '.join(missing_vars)}. Please check your .env file." + raise EnvironmentError(error_msg) + # Register OpenMemory service factory for openmemory:// URI scheme def openmemory_factory(uri: str, **kwargs): parsed = urlparse(uri) location = parsed.netloc + parsed.path base_url = location if location.startswith(('http://', 'https://')) else f'http://{location}' - api_key = os.getenv('OPENMEMORY_API_KEY', '') - if not api_key: - raise ValueError("OpenMemory API key required. Set OPENMEMORY_API_KEY environment variable.") + + api_key = os.getenv('OPENMEMORY_API_KEY') return OpenMemoryService(base_url=base_url, api_key=api_key) get_service_registry().register_memory_service("openmemory", openmemory_factory) -# Build OpenMemory URI from environment variables (API key comes from env var) -base_url = os.getenv('OPENMEMORY_BASE_URL', 'http://localhost:8080').replace('http://', '').replace('https://', '') -MEMORY_SERVICE_URI = f"openmemory://{base_url}" +# Build OpenMemory URI from environment variables +raw_base_url = os.getenv('OPENMEMORY_BASE_URL', 'localhost:8080') +# Clean the URL to ensure it works with the URI scheme parser +clean_base_url = raw_base_url.replace('http://', '').replace('https://', '') +MEMORY_SERVICE_URI = f"openmemory://{clean_base_url}" +# Run validation before creating the app +validate_environment() # Create the FastAPI app using get_fast_api_app app: FastAPI = get_fast_api_app(