Skip to content

fix: enhance PR title generation with retries and better errors#243

Merged
appleboy merged 1 commit intomainfrom
retry
May 25, 2025
Merged

fix: enhance PR title generation with retries and better errors#243
appleboy merged 1 commit intomainfrom
retry

Conversation

@appleboy
Copy link
Owner

@appleboy appleboy commented May 25, 2025

  • Add retry logic for generating the pull request title, retrying up to 3 times if the response is empty
  • Return an explicit error if the OpenAI API returns no choices, or if reasoning content is present (unsupported)
  • Improve error reporting for failed title generation attempts

fix #242

Summary by CodeRabbit

  • New Features
    • Improved pull request title generation with automatic retries for empty results, ensuring more reliable titles.
  • Bug Fixes
    • Enhanced error handling for AI-generated chat completions, including checks for empty responses and unsupported reasoning content.

@appleboy appleboy requested a review from Copilot May 25, 2025 04:44
@coderabbitai
Copy link

coderabbitai bot commented May 25, 2025

Walkthrough

The changes introduce improved error handling and validation in the AI-based pull request title generation process and OpenAI chat completion client. Retry logic is added for empty AI-generated titles, and the OpenAI client now checks for empty or unsupported responses, returning errors accordingly.

Changes

File(s) Change Summary
cmd/commit.go Added retry logic for AI-generated PR titles, ensuring non-empty output and error reporting after retries fail.
provider/openai/openai.go Enhanced chat completion: validated response for empty choices and disallowed reasoning content in the first choice.

Assessment against linked issues

Objective Addressed Explanation
Prevent "index out of range" panic by validating empty AI responses (#242)
Add robust error handling for missing or invalid AI-generated pull request titles (#242)
Ensure OpenAI client handles empty/unsupported responses gracefully (#242)

Poem

A rabbit hopped and fixed a bug,
Where empty thoughts once gave a shrug.
With retries, checks, and careful code,
No more panics on this road!
Now titles bloom with every run—
This patch is done, the fix is won!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 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 cf5b839 and 7c011d1.

📒 Files selected for processing (2)
  • cmd/commit.go (2 hunks)
  • provider/openai/openai.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cmd/commit.go
  • provider/openai/openai.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: windows-latest @ Go stable
  • GitHub Check: Analyze (go)
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the robustness and validation around OpenAI-based title generation by adding retry and trimming logic, explicit error handling, and disabling reasoning.

  • Add retry logic (up to 3 attempts) with a brief pause when the generated title is empty
  • Trim whitespace from the AI-generated title and normalize casing
  • Handle cases where the API returns no choices or unwanted reasoning content, and disable the thinking flag in chat templates

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
provider/openai/openai.go Validate that r.Choices is non-empty, reject reasoning outputs, and set enable_thinking
cmd/commit.go Implement retry loop for empty titles, trim and normalize the title, and surface errors
Comments suppressed due to low confidence (3)

cmd/commit.go:190

  • Indexing a UTF-8 string by byte can break on multibyte characters; use rune slicing or the unicode package to lowercase the first character safely.
summarizeTitle = strings.TrimRight(strings.ToLower(string(summarizeTitle[0]))+summarizeTitle[1:], ".")

provider/openai/openai.go:186

  • New logic that handles zero-choice responses should be covered by unit tests to verify correct error handling when the API returns no choices.
if len(r.Choices) == 0 {

provider/openai/openai.go:187

  • [nitpick] This error could include more context (e.g., model name, request ID) to aid in debugging when no choices are returned.
return nil, fmt.Errorf("no choices returned from API")

Copy link

@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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 37fa5a3 and cf5b839.

📒 Files selected for processing (2)
  • cmd/commit.go (2 hunks)
  • provider/openai/openai.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: windows-latest @ Go stable
  • GitHub Check: Analyze (go)
  • GitHub Check: windows-latest @ Go stable
🔇 Additional comments (3)
cmd/commit.go (1)

4-4: LGTM - Import addition is appropriate.

The fmt import is correctly added to support the new error formatting in the retry logic.

provider/openai/openai.go (2)

162-164: LGTM - Correct implementation of thinking mode disable.

The ChatTemplateKwargs configuration properly disables thinking mode as required by the PR objectives, ensuring the OpenAI API doesn't return reasoning content.


186-192: Excellent defensive programming - proper API response validation.

The validation improvements are well-implemented:

  1. Empty choices check: Prevents potential index out of bounds errors
  2. Reasoning content rejection: Aligns with PR objectives to disallow reasoning models
  3. Clear error messages: Help with debugging and user understanding

This validation complements the retry logic in cmd/commit.go by ensuring that invalid responses are caught early and handled appropriately.

- Add retry logic for generating the pull request title, retrying up to 3 times if the response is empty
- Return an explicit error if the OpenAI API returns no choices, or if reasoning content is present (unsupported)
- Improve error reporting for failed title generation attempts

fix #242

Signed-off-by: appleboy <[email protected]>
@appleboy appleboy changed the title fix: improve reliability and validation of OpenAI title generation fix: enhance PR title generation with retries and better errors May 25, 2025
@appleboy appleboy merged commit 6883cb3 into main May 25, 2025
15 checks passed
@appleboy appleboy deleted the retry branch May 25, 2025 04:58
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.

Panic: Runtime Error: index out of range

2 participants