Skip to content

fix(chat): guard against None file_objects and unbound result in generate_summary_from_files - #1444

Open
miso466 wants to merge 1 commit into
khoj-ai:masterfrom
miso466:fix/generate-summary-none-type-guard
Open

miso466 wants to merge 1 commit into
khoj-ai:masterfrom
miso466:fix/generate-summary-none-type-guard

Conversation

@miso466

@miso466 miso466 commented Sep 28, 2026

Copy link
Copy Markdown

Summary

Fix three related runtime crash paths in src/khoj/routers/helpers.py::generate_summary_from_files:

  1. TypeError: 'NoneType' object is not iterable on empty-agent file summaries:
    When an agent has no existing indexed entries (file_objects is None) but the user uploads/provides a file to summarize (query_files is set), the outer guard at line 780 passes as intended:

    if (file_objects and len(file_objects) == 0 and not query_files) or (not file_objects and not query_files):

    However, lines 785 and 793 attempt to iterate over file_objects directly:

    contextual_data = " ".join([f"File: {file.file_name}\n\n{file.raw_text}" for file in file_objects])
    file_names = [file.file_name for file in file_objects]

    This causes a runtime TypeError: 'NoneType' object is not iterable. Initializing file_objects = file_objects or [] resolves this.

  2. TypeError: 'NoneType' object is not callable when send_status_func is None:
    send_status_func has default None in the function signature, but line 801 called it unconditionally without checking if send_status_func:. Aligned with sibling functions in helpers.py (lines 835, 853, 1083, 1101, 1364) by wrapping with if send_status_func:.

  3. UnboundLocalError in outer exception handler:
    Line 818 attempted to yield result. result is only defined within the async for result in send_status_func(...) loop. When an exception occurs prior to or without status streaming, result is unbound, raising UnboundLocalError: cannot access local variable 'result' where it is not associated with a value. Corrected to yield response_log.

Verification

  • Created reproducer unit test suite exercising:
    • Summary with only query_files when file_objects is None -> passes and generates summary cleanly.
    • Summary with send_status_func -> properly streams status events.
    • Empty files and empty query_files -> returns early with "Sorry, I couldn't find anything to summarize.".
    • Exception handler path -> yields response_log without raising UnboundLocalError.
  • Verified python3 -m py_compile src/khoj/routers/helpers.py passes cleanly.

…rate_summary_from_files

Fix three related runtime crash paths in generate_summary_from_files:
1. When agent has no indexed entries (file_objects is None) but query_files is
   provided, default file_objects to an empty list to avoid
   TypeError: 'NoneType' object is not iterable at lines 785 and 793.
2. Guard send_status_func invocation with 'if send_status_func:' to prevent
   TypeError when caller passes send_status_func=None (matching sibling handlers).
3. In the outer exception handler, yield response_log instead of unbound result
   variable to avoid UnboundLocalError during error reporting.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant