Fix: BYOK with llama-stack as a library#1268
Conversation
When using a BYOK rag database and llama-stack as a library the service
blows up on start with:
```
yaml.constructor.ConstructorError: could not determine a constructor for the tag
'tag:yaml.org,2002:python/object/apply:pathlib._local.PosixPath'
in "<unicode string>", line 122, column 16:
db_path: !!python/object/apply:pathlib._l ...
```
And we can see in `/tmp/llama_stack_enriched_config.yaml`:
```
storage:
backends:
byok_rag_1_storage:
db_path: !!python/object/apply:pathlib._local.PosixPath
- /home/geguileo/osls/rags/llamastack/rag/vector_db/os_product_docs/faiss_store.db
type: kv_sqlite
```
This is caused by how we define the `db_path` in our model and how it is
being used:
In `src/models/config.py` we define it as a FilePath:
```
1563 db_path: FilePath = Field(
1564 ...,
1565 title="DB path",
1566 description="Path to RAG database.",
1567 )
```
And in `src/llama_stack_configuration.py` we use it as a `str`:
```
138 # add new backends for each BYOK RAG
139 for brag in byok_rag:
140 vector_db_id = brag.get("vector_db_id", "")
141 backend_name = f"byok_{vector_db_id}_storage"
142 output[backend_name] = {
143 "type": "kv_sqlite",
144 "db_path": brag.get("db_path", f".llama/{vector_db_id}.db"),
145 }
```
This patch fixes this in the simplest way, making the field in the BYOK
model match the other existing `db_path` field, in other words, changing
it to a `str`.
WalkthroughThe Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
tests/unit/models/config/test_byok_rag.py (1)
35-43:⚠️ Potential issue | 🟡 MinorOutdated docstring:
db_pathis no longer converted to a Path.The docstring at lines 41-42 states "db_path is converted to a Path", but with the type change from
FilePathtostr, this is no longer accurate. The value remains a string.📝 Proposed fix
def test_byok_rag_configuration_nondefault_values() -> None: """Test the ByokRag constructor. Verify that ByokRag class accepts and stores non-default configuration values. Asserts that rag_id, rag_type, embedding_model, embedding_dimension, and - vector_db_id match the provided inputs and that db_path is converted to a - Path. + vector_db_id match the provided inputs and that db_path is stored as a string. """🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/unit/models/config/test_byok_rag.py` around lines 35 - 43, Update the outdated docstring in test_byok_rag_configuration_nondefault_values: remove or reword the sentence claiming "db_path is converted to a Path" because db_path is now a str (FilePath → str) and remains a string; reference the ByokRag constructor and the db_path attribute in the docstring so the test description correctly states that db_path remains a string rather than being converted to a Path.
🧹 Nitpick comments (2)
tests/unit/models/config/test_byok_rag.py (1)
62-73: Consider using string literals fordb_pathconsistently across all tests.Some tests (lines 24, 51) pass
db_pathas a string, while others (lines 72, 88, 111, 127, 143) still passPath("tests/configuration/rag.txt"). While Pydantic will coercePathtostr, using consistent string literals would better reflect the model's actualstrtype and improve test clarity.♻️ Example fix for one test
with pytest.raises(ValidationError, match="should be greater than 0"): _ = ByokRag( rag_id="rag_id", rag_type="rag_type", embedding_model="embedding_model", embedding_dimension=-1024, vector_db_id="vector_db_id", - db_path=Path("tests/configuration/rag.txt"), + db_path="tests/configuration/rag.txt", )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/unit/models/config/test_byok_rag.py` around lines 62 - 73, Update the test to pass db_path as a string literal instead of a Path object for consistency with other tests: in test_byok_rag_configuration_wrong_dimension replace Path("tests/configuration/rag.txt") with the string "tests/configuration/rag.txt" when constructing the ByokRag instance so db_path usage across tests matches the model's str type (affects test_byok_rag_configuration_wrong_dimension and any other tests still using Path(...)).src/models/config.py (1)
1562-1566: Consider addingmin_length=1to prevent empty path strings.Other string fields in
ByokRag(e.g.,rag_id,rag_type,embedding_model,vector_db_id) havemin_length=1validation. An emptydb_pathwould likely cause runtime errors when llama-stack attempts to use it.🛡️ Proposed enhancement
db_path: str = Field( ..., + min_length=1, title="DB path", description="Path to RAG database.", )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/models/config.py` around lines 1562 - 1566, The db_path Field in the ByokRag model lacks a min_length constraint, allowing empty strings that can cause runtime errors; update the db_path Field declaration (the db_path: str = Field(...)) to include min_length=1 to match validations used by rag_id, rag_type, embedding_model, and vector_db_id so empty path strings are rejected at validation time.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@tests/unit/models/config/test_byok_rag.py`:
- Around line 35-43: Update the outdated docstring in
test_byok_rag_configuration_nondefault_values: remove or reword the sentence
claiming "db_path is converted to a Path" because db_path is now a str (FilePath
→ str) and remains a string; reference the ByokRag constructor and the db_path
attribute in the docstring so the test description correctly states that db_path
remains a string rather than being converted to a Path.
---
Nitpick comments:
In `@src/models/config.py`:
- Around line 1562-1566: The db_path Field in the ByokRag model lacks a
min_length constraint, allowing empty strings that can cause runtime errors;
update the db_path Field declaration (the db_path: str = Field(...)) to include
min_length=1 to match validations used by rag_id, rag_type, embedding_model, and
vector_db_id so empty path strings are rejected at validation time.
In `@tests/unit/models/config/test_byok_rag.py`:
- Around line 62-73: Update the test to pass db_path as a string literal instead
of a Path object for consistency with other tests: in
test_byok_rag_configuration_wrong_dimension replace
Path("tests/configuration/rag.txt") with the string
"tests/configuration/rag.txt" when constructing the ByokRag instance so db_path
usage across tests matches the model's str type (affects
test_byok_rag_configuration_wrong_dimension and any other tests still using
Path(...)).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 642c1707-1543-4b55-864e-547f2b378c14
📒 Files selected for processing (2)
src/models/config.pytests/unit/models/config/test_byok_rag.py
|
@Akrog Path can be converted to string automatically. Does it mean, that your path is not the real path? |
Description
When using a BYOK rag database and llama-stack as a library the service blows up on start with:
And we can see in
/tmp/llama_stack_enriched_config.yaml:This is caused by how we define the
db_pathin our model and how it is being used:In
src/models/config.pywe define it as a FilePath:And in
src/llama_stack_configuration.pywe use it as astr:This patch fixes this in the simplest way, making the field in the BYOK model match the other existing
db_pathfield, in other words, changing it to astr.Type of change
Tools used to create PR
None
Checklist before requesting a review
Testing
/tmp/llama_stack_enriched_config.yamllooks good and the service no longer blows up.Summary by CodeRabbit