Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix: robust api_key handling in semantic_extract module
KaifAhmad1f2a11fc8ad0c
diff --git a/semantica/semantic_extract/llm_extraction.py b/semantica/semantic_extract/llm_extraction.pyindex 2c0dd3c4..8c55ff86 100644--- a/semantica/semantic_extract/llm_extraction.py+++ b/semantica/semantic_extract/llm_extraction.py@@ -108,7 +108,12 @@ class LLMExtraction:# Initialize provider using new systemtry:- self.provider = create_provider(provider, **config)+ # Sanitize config: remove api_key if it's None/empty to allow fallback+ provider_config = config.copy()+ if "api_key" in provider_config and not provider_config["api_key"]:+ del provider_config["api_key"]++ self.provider = create_provider(provider, **provider_config)except Exception as e:self.logger.warning(f"Failed to initialize {provider} provider: {e}")self.provider = Nonediff --git a/semantica/semantic_extract/methods.py b/semantica/semantic_extract/methods.pyindex ab99e69c..c5edcdc9 100644--- a/semantica/semantic_extract/methods.py+++ b/semantica/semantic_extract/methods.py@@ -1563,18 +1563,24 @@ def extract_relations_llm(# Pass api_key if provided in kwargsprovider_kwargs = kwargs.copy()- if "api_key" not in provider_kwargs:++ # Check if api_key is provided but empty, or not provided at all+ if "api_key" not in provider_kwargs or not provider_kwargs["api_key"]:import osenv_key = f"{provider.upper()}_API_KEY"api_key = os.getenv(env_key)if api_key:provider_kwargs["api_key"] = api_key++ # Remove None/empty API key if still present to avoid provider errors+ if "api_key" in provider_kwargs and not provider_kwargs["api_key"]:+ del provider_kwargs["api_key"]# 2. PROVIDER VALIDATIONtry:llm = create_provider(provider, model=model, **provider_kwargs)if not llm.is_available():- error_msg = f"{provider} provider not available for relation extraction."+ error_msg = f"{provider} provider not available for relation extraction (key missing?)."logger.error(error_msg)if not silent_fail:raise ProcessingError(error_msg)@@ -2009,12 +2015,18 @@ def extract_triplets_llm(# Pass api_key if provided in kwargsprovider_kwargs = kwargs.copy()- if "api_key" not in provider_kwargs:++ # Check if api_key is provided but empty, or not provided at all+ if "api_key" not in provider_kwargs or not provider_kwargs["api_key"]:import osenv_key = f"{provider.upper()}_API_KEY"api_key = os.getenv(env_key)if api_key:provider_kwargs["api_key"] = api_key++ # Remove None/empty API key if still present to avoid provider errors+ if "api_key" in provider_kwargs and not provider_kwargs["api_key"]:+ del provider_kwargs["api_key"]# 2. PROVIDER VALIDATIONtry:diff --git a/semantica/semantic_extract/ner_extractor.py b/semantica/semantic_extract/ner_extractor.pyindex 4ab7267f..9a6d8890 100644--- a/semantica/semantic_extract/ner_extractor.py+++ b/semantica/semantic_extract/ner_extractor.py@@ -377,8 +377,10 @@ class NERExtractor:)# Pass api_key if provided (needed for all providers)if "api_key" in all_options:- method_options["api_key"] = all_options["api_key"]- elif "api_key" not in method_options:+ # Only pass if truthy to allow fallback+ if all_options["api_key"]:+ method_options["api_key"] = all_options["api_key"]+ if "api_key" not in method_options:# Try to get from environment as fallbackimport osprovider = method_options.get("provider", "openai")diff --git a/semantica/semantic_extract/relation_extractor.py b/semantica/semantic_extract/relation_extractor.pyindex d29bdcac..593622ea 100644--- a/semantica/semantic_extract/relation_extractor.py+++ b/semantica/semantic_extract/relation_extractor.py@@ -421,7 +421,9 @@ class RelationExtractor:)# Pass api_key if provided (needed for all providers)if "api_key" in all_options:- method_options["api_key"] = all_options["api_key"]+ # Only pass if truthy to allow fallback+ if all_options["api_key"]:+ method_options["api_key"] = all_options["api_key"]elif "api_key" not in method_options:# Try to get from environment as fallbackimport os@@ -440,6 +442,12 @@ class RelationExtractor:if verbose_mode and method_name == "llm":import sysprint(f" [RelationExtractor] Processing with {method_name}...", flush=True, file=sys.stdout)+ print(f" [RelationExtractor Debug] method_options keys: {list(method_options.keys())}", flush=True, file=sys.stdout)+ if "api_key" in method_options:+ masked = method_options["api_key"][:4] + "..." if method_options["api_key"] else "None"+ print(f" [RelationExtractor Debug] api_key present: {masked}", flush=True, file=sys.stdout)+ else:+ print(f" [RelationExtractor Debug] api_key NOT present", flush=True, file=sys.stdout)relations = method_func(text, entities, **method_options)diff --git a/semantica/semantic_extract/triplet_extractor.py b/semantica/semantic_extract/triplet_extractor.pyindex c7f3f9d9..9bb4feb0 100644--- a/semantica/semantic_extract/triplet_extractor.py+++ b/semantica/semantic_extract/triplet_extractor.py@@ -463,8 +463,10 @@ class TripletExtractor:)# Pass api_key if provided (needed for all providers)if "api_key" in all_options:- method_options["api_key"] = all_options["api_key"]- elif "api_key" not in method_options:+ # Only pass if truthy to allow fallback+ if all_options["api_key"]:+ method_options["api_key"] = all_options["api_key"]+ if "api_key" not in method_options:# Try to get from environment as fallbackimport osprovider = method_options.get("provider", "openai")