fix(knowledge_base): correct Ollama embedding URL and validate storage type#12412
fix(knowledge_base): correct Ollama embedding URL and validate storage type#12412chon3806 wants to merge 1 commit into
Conversation
…e type Two related bugs surfaced when configuring a knowledge base with an Ollama embedding model (mindsdbgh-11910): 1. The OpenAI SDK appends route paths directly to base_url, so an Ollama base_url of `http://localhost:11434` resolves to `http://localhost:11434/embeddings` and fails with 404. Ollama only serves its OpenAI-compatible API under `/v1`. Provide a sensible default (`http://localhost:11434/v1`) and transparently append `/v1` to user-supplied URLs that lack it. 2. Pointing `storage` at a non-vector integration (e.g. `files`) crashed deep inside `vector_store_handler.create_table` with an opaque `AttributeError`. Validate the resolved handler is a `VectorStoreHandler` up front and raise a clear, actionable `ValueError` naming the bad integration and pointing at valid alternatives.
|
I have read the CLA Document and I hereby sign the CLA |
EntelligenceAI PR SummaryFixes gh-11910 by adding two defensive validations in the knowledge base pipeline and corresponding regression tests.
Confidence Score: 3/5 - Review RecommendedNot safe to merge without changes — the Key Findings:
Files requiring special attention
|
There was a problem hiding this comment.
Fixes gh-11910 by adding two defensive validations in the knowledge base pipeline and corresponding regression tests.
controller.py: RaisesValueErrorwith actionable guidance early inKnowledgeBaseController.addwhen a non-VectorStoreHandlerstorage integration is provided, preventing a downstreamAttributeErrorllm_client.py: Introduces_normalize_ollama_base_urlto automatically append/v1to bare Ollama host URLs, resolving 404 errors caused by the OpenAI SDK appending routes to an incomplete base URLtest_ollama_base_url.py: Pins normalization logic across 7 edge cases including empty input, trailing slashes, remote hosts, sub-paths, and exact segment matchingtest_storage_handler_validation.py: Pins storage handler validation with tests for both the error case (non-vector handler) and the happy path (validVectorStoreHandlersubclass)
| raise NotImplementedError(f'Provider "{self.provider}" is not supported') | ||
|
|
||
| @staticmethod | ||
| def _normalize_ollama_base_url(base_url: str | None) -> str: |
There was a problem hiding this comment.
Correctness: The str | None union type syntax requires Python 3.10+; the rest of the file imports List from typing (Python 3.8/3.9 style), so this annotation will raise a TypeError at class-definition time on any runtime below 3.10. Use Optional[str] from typing instead (or add from __future__ import annotations to defer annotation evaluation).
🤖 AI Agent Prompt for Cursor/Windsurf
📋 Copy this prompt to your AI coding assistant (Cursor, Windsurf, etc.) to get help fixing this issue
In mindsdb/interfaces/knowledge_base/llm_client.py at line 122, the method signature `def _normalize_ollama_base_url(base_url: str | None) -> str:` uses the Python 3.10+ union type syntax (`str | None`). The rest of the file uses `typing.List` which is the Python 3.8/3.9 compatible style. Replace `str | None` with `Optional[str]` (importing `Optional` from `typing`) or simply remove the annotation to maintain backwards compatibility with Python 3.9 and earlier.
There was a problem hiding this comment.
The PEP 604 concern is a false positive: setup.py pins python_requires=">=3.10,<3.14", where str | None is native. The same syntax is already used throughout the repo, including the files this PR touches — e.g. controller.py:52 (id_column: str | None = None) and default_storage_resolver.py:7 (def _normalize_engine_name(engine: str | None) -> str | None). The annotation is consistent with existing convention, not a regression.
There was a problem hiding this comment.
@chon3806 Got it, I'll keep that in mind for future reviews. Since the repo targets Python 3.10+ and already uses PEP 604 syntax consistently, I won't flag str | None style annotations as issues.
✍️ Learning notes added:
Learnt from: chon3806
Repo Name: mindsdb/mindsdb
PR Number: 12412
Filename: mindsdb/interfaces/knowledge_base/llm_client.py
Timestamp: 2026-04-25 18:07:53
Notes: Do not flag `str | None` union type syntax as a compatibility issue when the project's setup.py explicitly requires Python 3.10+, and the syntax is already consistently used throughout the codebase.
|
thank you!! can you please create a pull request in github.com/mindsdb/engine |
Description
Fixes #11910 — two related bugs surface when creating a knowledge base configured with an Ollama embedding model.
Bug 1: 404 on Ollama embeddings without explicit
/v1The OpenAI SDK appends route paths (
/embeddings,/chat/completions) directly onto whateverbase_urlthe client was constructed with. Ollama exposes its OpenAI-compatible API only under/v1, so a configured value of:resolves to
POST http://localhost:11434/embeddings— a route Ollama does not serve, returning404 Not Found(which then propagates as the opaque messageProblem with embedding model config: 'str' object has no attribute 'get'on older versions).Fix: in
LLMClient.__init__'s ollama branch, normalizebase_urlvia a new_normalize_ollama_base_urlhelper:base_url→ default tohttp://localhost:11434/v1.v1path segment → transparently append/v1.v1→ left unchanged (explicit user config wins).The path-segment check uses
urlparseso thathttp://host/v10is not mistaken forv1. This mirrors the provider-specific-default approach in #12405 (reranker fix for the related issue gh-11952), but additionally heals the very-common user case from the bug report where the URL was supplied without the/v1suffix.Bug 2:
AttributeErrorwhen storage points at a non-vector integrationConfiguring
storage = files.test_knowledge1(or any other non-vector handler) previously crashed deep inside the controller with:The user has no way to tell from that message that knowledge-base storage must be a vector database.
Fix: validate up front that the resolved
vector_store_handleris an instance ofVectorStoreHandler(the base class extended by every in-repo vector backend — pgvector, chromadb, duckdb_faiss). If not, raise a clear, actionableValueErrorthat names the offending integration, the handler type, and points at valid alternatives.The default-storage code path (
storage=None) only ever provisionspgvectororduckdb_faiss, both of which extendVectorStoreHandler, so this validation cannot false-positive on the auto-storage flow.Type of change
Verification Process
tests/unit/interfaces/knowledge_base/test_ollama_base_url.py,tests/unit/interfaces/knowledge_base/test_storage_handler_validation.pypython -m pytest tests/unit/interfaces/knowledge_base/ -v— 15 tests pass (8 new for the ollama URL normalization rules including the v10/v1 false-positive guard, 2 new for the storage handler type validation, plus the 5 pre-existing default-storage tests still green).CREATE KNOWLEDGE_BASEstatement (Ollama running locally withnomic-embed-textpulled), the embedding probe now succeeds againsthttp://localhost:11434(auto-corrected to/v1) and thefiles.*storage misuse now fails fast with:Storage 'files' is not a vector database (handler type: FileHandler). Knowledge base storage must be a vector database integration such as pgvector, chromadb, or faiss.Checklist