From 2f05ff2a9677d3196f909e8367f0efbea4a1a634 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Tue, 17 Mar 2026 18:19:02 -0600 Subject: [PATCH] Remove extra details from ollama schema --- frigate/genai/ollama.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/frigate/genai/ollama.py b/frigate/genai/ollama.py index 90bf3f05e..141b297a9 100644 --- a/frigate/genai/ollama.py +++ b/frigate/genai/ollama.py @@ -53,6 +53,39 @@ class OllamaClient(GenAIClient): logger.warning("Error initializing Ollama: %s", str(e)) return None + @staticmethod + def _clean_schema_for_ollama(schema: dict) -> dict: + """Strip Pydantic metadata from a JSON schema for Ollama compatibility. + + Ollama's grammar-based constrained generation works best with minimal + schemas. Pydantic adds title/description/constraint fields that can + cause the grammar generator to silently skip required fields. + """ + STRIP_KEYS = { + "title", + "description", + "minimum", + "maximum", + "exclusiveMinimum", + "exclusiveMaximum", + } + result = {} + for key, value in schema.items(): + if key in STRIP_KEYS: + continue + if isinstance(value, dict): + result[key] = OllamaClient._clean_schema_for_ollama(value) + elif isinstance(value, list): + result[key] = [ + OllamaClient._clean_schema_for_ollama(item) + if isinstance(item, dict) + else item + for item in value + ] + else: + result[key] = value + return result + def _send( self, prompt: str, @@ -73,7 +106,9 @@ class OllamaClient(GenAIClient): if response_format and response_format.get("type") == "json_schema": schema = response_format.get("json_schema", {}).get("schema") if schema: - ollama_options["format"] = schema + ollama_options["format"] = self._clean_schema_for_ollama( + schema + ) result = self.provider.generate( self.genai_config.model, prompt,