Skip to content

Fix web search fallback and citation parsing for KB search - #17

Merged
kiranharidas187 merged 3 commits into
ELEVATE-Project:release-1.1.1from
darshilbabel:drop_8_fixes
Jul 21, 2026
Merged

Fix web search fallback and citation parsing for KB search#17
kiranharidas187 merged 3 commits into
ELEVATE-Project:release-1.1.1from
darshilbabel:drop_8_fixes

Conversation

@KUNALTEMPEST

@KUNALTEMPEST KUNALTEMPEST commented Jul 20, 2026

Copy link
Copy Markdown
  • Make the KB-empty fallback message directive ("call web_search now") instead of giving the LLM an easy out to answer from general knowledge
  • Inject an assistant-role nudge into the gateway conversation whenever the web search fallback activates, reinforcing the tool call
  • Fix _extract_citation_chunks to handle the actual Anthropic/litellm citation shapes (nested web_search_tool_result.content[], and the provider_specific_fields fallback when citations is null) — previously every citation was silently dropped due to a shape mismatch

Summary by CodeRabbit

  • Bug Fixes
    • Improved fallback behavior when the knowledge base can’t answer; web search is now guided more explicitly, and responses are more accurate.
    • Enhanced citation and web-search result extraction across additional provider response formats.
    • Improved messaging when no knowledge-base results are available and web search is disabled.
  • API & Integration
    • Added more robust handling for Elevate profile/T&C errors and unexpected upstream responses.
    • Disabled the /api/profile/ and /api/get-ip-location/ endpoints.

- Make the KB-empty fallback message directive ("call web_search now")
  instead of giving the LLM an easy out to answer from general knowledge
- Inject an assistant-role nudge into the gateway conversation whenever
  the web search fallback activates, reinforcing the tool call
- Fix _extract_citation_chunks to handle the actual Anthropic/litellm
  citation shapes (nested web_search_tool_result.content[], and the
  provider_specific_fields fallback when citations is null) — previously
  every citation was silently dropped due to a shape mismatch
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bb2e9d18-6e2c-4c2c-8c2d-ef0c234b1533

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The response handler now directs the model to use web search after empty KB results and extracts additional citation formats. Elevate profile and T&C flows now handle configuration, authorization, upstream, and response-code errors explicitly, while two API routes are disabled.

Changes

Web-search response handling

Layer / File(s) Summary
KB fallback instructions
chatbot/services/response_handlers/base_response_handler.py
Empty KB results produce web-search directives when enabled, append an assistant instruction before the next tool-loop iteration, or allow general-knowledge responses when web search is disabled.
Expanded citation extraction
chatbot/services/response_handlers/base_response_handler.py
Non-stream responses parse nested citation content and fall back to provider_specific_fields.web_search_results when standard citations yield no chunks.

Elevate profile handling

Layer / File(s) Summary
Elevate update error handling
chatbot/utils/elevate/profile_utils.py
Profile updates validate ELEVATE_BASE_URL, handle upstream status codes, validate Elevate’s response code, and return structured errors for request and unexpected failures.
T&C view integration
chatbot/views/api_views.py
Profile retrieval and T&C acceptance explicitly handle Elevate authorization and server errors and derive or return T&C acceptance status from Elevate responses.

Disabled API routes

Layer / File(s) Summary
Removed URL patterns
chatbot/urls.py
The /api/profile/ and /api/get-ip-location/ URL patterns are commented out and no longer active.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ResponseHandler
  participant KnowledgeBase
  participant GatewayLLM
  participant WebSearch
  ResponseHandler->>KnowledgeBase: search_knowledge_base
  KnowledgeBase-->>ResponseHandler: return no chunks
  ResponseHandler->>GatewayLLM: append web_search instruction
  GatewayLLM->>WebSearch: invoke web_search
  WebSearch-->>ResponseHandler: return search results and citations
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main KB web-search fallback and citation parsing changes in the pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@KUNALTEMPEST
KUNALTEMPEST changed the base branch from release-1.1.0 to release-1.1.1 July 20, 2026 10:52
@KUNALTEMPEST

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@chatbot/services/response_handlers/base_response_handler.py`:
- Around line 822-859: Update the citation extraction around
_collect_from_tool_results and the flat citation loop to obtain each excerpt
from the provider’s citation block rather than web_search_tool_result.content
items, which lack cited_text. Preserve the existing title and URL extraction,
and populate each chunk’s text with the corresponding quoted citation excerpt
for both Anthropic and fallback provider payloads.
- Around line 501-510: Update the fallback branch in the response-handling flow
around tool_name == 'search_knowledge_base' so the web-search nudge uses a
provider-supported prefill or continuation mechanism rather than appending a
plain assistant message. Gate this behavior to the Anthropic provider if no
generic backend-supported mechanism exists, while preserving the existing
condition requiring use_web_search and no new_chunks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0f43108c-f9ae-40f7-98b4-1cc849256e7e

📥 Commits

Reviewing files that changed from the base of the PR and between 5e6ec2e and af0d940.

📒 Files selected for processing (1)
  • chatbot/services/response_handlers/base_response_handler.py

Comment thread chatbot/services/response_handlers/base_response_handler.py
Comment thread chatbot/services/response_handlers/base_response_handler.py
- Disable /api/profile/ and /api/get-ip-location/ routes: the former
  allowed unvalidated profile creation (arbitrary email/password/
  other_params), the latter is unused.
- get-profile and accept-tnc now surface Elevate's actual error and
  status code (401 unauthorized, 5xx service error) instead of
  silently defaulting or always returning 502, matching the pattern
  already used in logout.
- update_elevate_profile returns structured {error, status_code}
  like fetch_elevate_user/logout_elevate_user instead of a bare {}.
- Drop local access_token pre-checks in get-profile/accept-tnc; a
  missing token now surfaces as Elevate's own 401 rather than a
  locally synthesized 400.
@KUNALTEMPEST

Copy link
Copy Markdown
Author

@coderabbitai review, please review latest push

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

@KUNALTEMPEST I’ll review the latest push.

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@chatbot/utils/elevate/profile_utils.py`:
- Around line 209-212: Update the responseCode validation in the profile update
flow to safely handle an explicit null value before calling lower(), while
preserving the existing unexpected-response logging and response.status_code
return. Ensure missing, null, and non-"ok" response codes follow the
elevate_server_error branch without reaching the broad exception handler.
- Around line 175-178: Add an early missing-access-token guard to both
fetch_elevate_user and update_elevate_profile before constructing Elevate
request headers, reusing each function’s existing unauthorized response so
callers receive 401 instead of triggering InvalidHeader and a 502. The affected
api_views.py sites at lines 226-233 and 301-309 require no direct changes; they
should continue propagating the utility functions’ unauthorized results.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3065eeee-ae4a-4eac-9510-898707d952f2

📥 Commits

Reviewing files that changed from the base of the PR and between af0d940 and 67e40ab.

📒 Files selected for processing (3)
  • chatbot/urls.py
  • chatbot/utils/elevate/profile_utils.py
  • chatbot/views/api_views.py

Comment thread chatbot/utils/elevate/profile_utils.py
Comment thread chatbot/utils/elevate/profile_utils.py
@kiranharidas187
kiranharidas187 merged commit 9821f00 into ELEVATE-Project:release-1.1.1 Jul 21, 2026
1 check passed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 14, 2026
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.

2 participants