Skip to content

Conversation

@paopa
Copy link
Contributor

@paopa paopa commented Mar 4, 2025

This PR adds tracking of invalid SQL queries in the AskResultResponse model and updates the error handling logic to capture and return invalid SQL statements.

Changes:

  • Added invalid_sql field to AskResultResponse model
  • Updated error handling to capture invalid SQL statements from failed dry runs
  • Improved code formatting for better readability
  • Fixed error message handling in SQL correction flow

Summary by CodeRabbit

  • New Features

    • Enhanced error feedback now provides additional context when SQL generation fails, offering details about the problematic query and corresponding error message.
  • Style

    • Minor formatting adjustments for improved clarity.

@paopa paopa added module/ai-service ai-service related ci/ai-service ai-service related labels Mar 4, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2025

Walkthrough

The changes update the SQL handling in the ask service by introducing a new optional attribute, invalid_sql, in the AskResultResponse class. In the ask method, logic has been added to capture details of failed SQL generation attempts by extracting the invalid SQL and an error message from failed_dry_run_results. Minor formatting adjustments were also made, such as using parentheses for assignment.

Changes

File Change Summary
wren-ai-service/.../ask.py Added invalid_sql: Optional[str] to AskResultResponse. Modified ask method to initialize invalid_sql and error_message as None and later populate them upon SQL generation failure. Adjusted formatting with parenthesis assignment.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant AskService
    participant SQLGenerator

    Client->>AskService: Call ask() method
    AskService->>SQLGenerator: Request SQL generation
    SQLGenerator-->>AskService: Return failed run result (invalid SQL & error message)
    AskService->>AskService: Extract invalid_sql & error_message
    AskService-->>Client: Respond with AskResultResponse (includes invalid_sql)
Loading

Possibly related PRs

Suggested reviewers

  • cyyeh

Poem

I'm a happy rabbit, hopping with delight,
Code paths have changed under the moonlight.
Invalid SQL now gets a friendly name,
Error messages join in the fun game.
With each new line, our code takes flight.
🐇✨
Celebrate the change, day and night!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4404ffc and 3fb0601.

📒 Files selected for processing (1)
  • wren-ai-service/src/web/v1/services/ask.py (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • wren-ai-service/src/web/v1/services/ask.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
wren-ai-service/src/web/v1/services/ask.py (1)

469-471: Consider extracting this logic to a helper function.

This extraction logic is duplicated in multiple places in the code (here and in lines 473-475). Consider creating a helper function to avoid repetition.

+def _extract_invalid_sql_info(failed_result):
+    return failed_result["sql"], failed_result["error"]
+
-invalid = failed_dry_run_results[0]
-invalid_sql = invalid["sql"]
-error_message = invalid["error"]
+invalid_sql, error_message = _extract_invalid_sql_info(failed_dry_run_results[0])
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 236c87c and 4404ffc.

📒 Files selected for processing (1)
  • wren-ai-service/src/web/v1/services/ask.py (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: pytest
  • GitHub Check: pytest
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
🔇 Additional comments (7)
wren-ai-service/src/web/v1/services/ask.py (7)

99-99: Excellent addition for debugging!

Adding the invalid_sql field to the AskResultResponse class will significantly improve error reporting and debugging capabilities. This allows clients to access the problematic SQL statement directly when queries fail, making troubleshooting more efficient.


376-377: Good practice to initialize variables early.

Explicitly initializing these variables at the beginning of the code block improves readability and prevents potential issues with undefined variables later in the function.


473-475: Duplicate extraction logic.

This is the same extraction logic used earlier in the code. Using the proposed helper function would also apply here.


504-504: Good implementation of the invalid_sql field.

Properly passing the captured invalid SQL to the response ensures clients receive the detailed error information. This enhances the debugging experience for developers working with the API.


643-647: Improved code formatting.

Using parentheses for multiline assignments makes the code more readable and consistent with modern Python style.


716-720: Consistent formatting approach.

Applying the same formatting style throughout the codebase improves maintainability. This change aligns with the previous formatting improvements.


376-505:

Details

❓ Verification inconclusive

Verify error handling consistency.

The improvements for tracking invalid SQL are only implemented for the AskResultResponse but not for AskFeedbackResultResponse. Consider whether similar tracking of invalid SQL should be added to the feedback flow for consistency.


🏁 Script executed:

#!/bin/bash
# Verify if there are instances where AskFeedbackResultResponse might benefit from having invalid_sql field

# Check for failure cases in the feedback flow
rg -A 5 "invalid_generation_results.*feedback" wren-ai-service/src/web/v1/services/ask.py

Length of output: 91


Consistently track SQL errors across response flows.

The current implementation tracks invalid SQL in the main response via the AskResultResponse, but our verification indicates that no similar handling is applied to the feedback flow (i.e. within AskFeedbackResultResponse). Please review the feedback pipeline to determine if incorporating an invalid_sql (and related error data) field there could improve error diagnostics and overall consistency.

@paopa paopa force-pushed the feat/return-invalid-sql-if-correcting-fails branch from 4404ffc to 3fb0601 Compare March 4, 2025 08:27
@paopa paopa requested a review from wwwy3y3 March 4, 2025 08:27
@paopa paopa merged commit 226259a into main Mar 5, 2025
10 checks passed
@paopa paopa deleted the feat/return-invalid-sql-if-correcting-fails branch March 5, 2025 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci/ai-service ai-service related module/ai-service ai-service related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants