From c069dd276e419caa5b662390cdf3c3192f278af5 Mon Sep 17 00:00:00 2001 From: Daulet Amirkhanov Date: Fri, 7 Nov 2025 16:51:11 +0000 Subject: [PATCH] feat: add model validator to strip quotes from string fields in LLMConfig --- cognee/infrastructure/llm/config.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cognee/infrastructure/llm/config.py b/cognee/infrastructure/llm/config.py index 8fd196eaf..dab2fa6c0 100644 --- a/cognee/infrastructure/llm/config.py +++ b/cognee/infrastructure/llm/config.py @@ -73,6 +73,26 @@ class LLMConfig(BaseSettings): model_config = SettingsConfigDict(env_file=".env", extra="allow") + @model_validator(mode="after") + def strip_quotes_from_strings(self) -> "LLMConfig": + """ + Strip surrounding quotes from all string fields in the model. + + This handles cases where Docker's --env-file or shell quoting + accidentally includes quotes in the value (e.g., LLM_API_KEY="value"). + + Returns: + LLMConfig: The instance with quotes stripped from all string fields. + """ + for field_name, _ in self.__class__.model_fields.items(): + value = getattr(self, field_name, None) + if isinstance(value, str) and len(value) >= 2: + if (value.startswith('"') and value.endswith('"')) or ( + value.startswith("'") and value.endswith("'") + ): + setattr(self, field_name, value[1:-1]) + return self + def model_post_init(self, __context) -> None: """Initialize the BAML registry after the model is created.""" # Check if BAML is selected as structured output framework but not available