Fix KeyError on missing image/docx keys in configure_content (#1317)#1338
Open
eldar702 wants to merge 1 commit into
Open
Fix KeyError on missing image/docx keys in configure_content (#1317)#1338eldar702 wants to merge 1 commit into
eldar702 wants to merge 1 commit into
Conversation
…#1317) configure_content guarded the image and docx indexing branches with a bracket lookup (files["image"] / files["docx"]) while every other content type — and even the bodies of these same two blocks — used the safe files.get(...). On a fresh install with the default t=all, files often lacks these keys, so the guard raised KeyError. The inner try/except swallowed it into success=False, surfacing as HTTP 500 "Failed to update content index" from GET /api/update. Use files.get("image") / files.get("docx") in the guards to match the convention already used throughout the function. Add DB-free regression tests asserting configure_content returns True when the image/docx keys are absent. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1317.
Problem
GET /api/updatereturns HTTP 500 (Failed to update content index) on a fresh install with the defaultt=all. The inner handler logsFailed to setup images: 'image'/Failed to setup docx: 'docx', setssuccess=False, and the outer caller raises 500. Scoping to a single type such ast=markdownavoids the crash.Fix
In
src/khoj/routers/helpers.py::configure_content(), the image and docx guard conditions used a bracket lookup on a possibly-missing key (files["image"],files["docx"]), while every other content type — and even the bodies of these same two blocks — already used the safefiles.get(...). Whenfileslacks those keys the guard raisesKeyError. The fix changes the two guards tofiles.get("image")/files.get("docx"), matching the convention used throughout the function (1 file, no new deps, no behavior change on the happy path).Test
Added two DB-free regression tests in
tests/test_helpers.py. Each callsconfigure_contentwith afilesdict that omits theimage/docxkeys (and a non-empty unrelated key so the Github/Notion server-side branches, which hit the DB, are skipped) and asserts it returnsTrue. Both fail before the fix (KeyError→success=False) and pass after.Verification
pytest tests/test_helpers.py -k configure_content— green (full file: 6 passed, 2 skipped — network tests skipped without API keys)mypy src/khoj/routers/helpers.py— no new errors introduced (pre-existing baseline unchanged)ruff check/ruff format --checkon touched lines — clean🤖 AI-assistance disclosure: drafted with assistance from Claude (Opus 4.8). All changes reviewed for correctness before submission.