Skip to content

Refine Gemini errors handling#349

Merged
KaQuMiQ merged 1 commit intomainfrom
feature/gemini_handling
Jul 1, 2025
Merged

Refine Gemini errors handling#349
KaQuMiQ merged 1 commit intomainfrom
feature/gemini_handling

Conversation

@KaQuMiQ
Copy link
Collaborator

@KaQuMiQ KaQuMiQ commented Jul 1, 2025

No description provided.

@coderabbitai
Copy link

coderabbitai bot commented Jul 1, 2025

Warning

Rate limit exceeded

@KaQuMiQ has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 44 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between c3e4957 and f73af54.

📒 Files selected for processing (10)
  • pyproject.toml (1 hunks)
  • src/draive/__init__.py (2 hunks)
  • src/draive/conversation/completion/default.py (6 hunks)
  • src/draive/gemini/config.py (1 hunks)
  • src/draive/gemini/lmm.py (1 hunks)
  • src/draive/gemini/lmm_generation.py (6 hunks)
  • src/draive/gemini/types.py (1 hunks)
  • src/draive/lmm/__init__.py (4 hunks)
  • src/draive/lmm/helpers.py (1 hunks)
  • src/draive/lmm/types.py (3 hunks)

Walkthrough

This change introduces new exception classes and a runtime-checkable protocol for output decoding to the draive.lmm package, exposing them as part of the public API along with a new helper function lmm_output_decoder that centralizes output decoding logic. The Gemini LMM generation code is refactored to unify request configuration, error handling, and observability, and to support a new optional thinking_budget parameter. Functions related to output format handling and tool configuration in Gemini LMM integration are removed, consolidating logic into streamlined methods. The GeminiException class now inherits from the base LMMException for consistent exception hierarchy.

✨ 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

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

🔭 Outside diff range comments (1)
src/draive/lmm/helpers.py (1)

74-83: Add error handling for model decoding

The model conversion could fail if the content doesn't match the expected schema. Consider adding error handling.

 def _prepare_model_output_conversion(
     model: type[DataModel],
 ) -> LMMOutputDecoder:
     def _model_output_conversion(
         content: MultimodalContent,
     ) -> MultimodalContent:
-        return MultimodalContent.of(model.from_json(content.to_str()))
+        try:
+            return MultimodalContent.of(model.from_json(content.to_str()))
+        except ValueError as exc:
+            from draive.lmm.types import LMMOutputInvalid
+            raise LMMOutputInvalid(f"Failed to decode {model.__name__} output") from exc
 
     return _model_output_conversion
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ea833c5 and 11614a4.

📒 Files selected for processing (8)
  • src/draive/__init__.py (2 hunks)
  • src/draive/gemini/config.py (1 hunks)
  • src/draive/gemini/lmm.py (1 hunks)
  • src/draive/gemini/lmm_generation.py (6 hunks)
  • src/draive/gemini/types.py (1 hunks)
  • src/draive/lmm/__init__.py (4 hunks)
  • src/draive/lmm/helpers.py (1 hunks)
  • src/draive/lmm/types.py (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.py`: Follow Ruff import ordering (standard library, third party, local) Us...

**/*.py: Follow Ruff import ordering (standard library, third party, local)
Use Python 3.12+ type features (type unions with |, generic syntax)
Use base and abstract types like Sequence or Iterable instead of concrete types
Use custom exceptions for specific errors
Format code with Ruff
Run linters (Ruff + Bandit + Pyright strict mode)

📄 Source: CodeRabbit Inference Engine (CLAUDE.md)

List of files the instruction was applied to:

  • src/draive/gemini/types.py
  • src/draive/gemini/config.py
  • src/draive/__init__.py
  • src/draive/lmm/helpers.py
  • src/draive/gemini/lmm.py
  • src/draive/lmm/types.py
  • src/draive/lmm/__init__.py
  • src/draive/gemini/lmm_generation.py
`**/__init__.py`: Put exported symbols into __init__.py

**/__init__.py: Put exported symbols into init.py

📄 Source: CodeRabbit Inference Engine (CLAUDE.md)

List of files the instruction was applied to:

  • src/draive/__init__.py
  • src/draive/lmm/__init__.py
🧠 Learnings (4)
src/draive/gemini/types.py (1)
Learnt from: CR
PR: miquido/draive#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-01T09:39:42.979Z
Learning: Applies to **/*.py : Use custom exceptions for specific errors
src/draive/__init__.py (1)
Learnt from: KaQuMiQ
PR: miquido/draive#338
File: src/draive/bedrock/lmm_generation.py:102-107
Timestamp: 2025-06-16T09:44:08.688Z
Learning: LMMInstruction is a type alias for str in the draive codebase, so LMMInstruction and str are equivalent types and can be used interchangeably.
src/draive/lmm/types.py (1)
Learnt from: KaQuMiQ
PR: miquido/draive#338
File: src/draive/bedrock/lmm_generation.py:102-107
Timestamp: 2025-06-16T09:44:08.688Z
Learning: LMMInstruction is a type alias for str in the draive codebase, so LMMInstruction and str are equivalent types and can be used interchangeably.
src/draive/lmm/__init__.py (1)
Learnt from: KaQuMiQ
PR: miquido/draive#338
File: src/draive/bedrock/lmm_generation.py:102-107
Timestamp: 2025-06-16T09:44:08.688Z
Learning: LMMInstruction is a type alias for str in the draive codebase, so LMMInstruction and str are equivalent types and can be used interchangeably.
🧬 Code Graph Analysis (5)
src/draive/gemini/types.py (1)
src/draive/lmm/types.py (1)
  • LMMException (121-122)
src/draive/__init__.py (1)
src/draive/lmm/types.py (2)
  • LMMOutputInvalid (129-130)
  • LMMOutputLimit (125-126)
src/draive/lmm/helpers.py (3)
src/draive/lmm/types.py (10)
  • LMMOutputDecoder (184-188)
  • of (86-109)
  • of (145-154)
  • of (165-174)
  • of (196-212)
  • of (223-232)
  • of (240-254)
  • of (264-275)
  • of (289-300)
  • of (355-365)
src/draive/multimodal/content.py (2)
  • MultimodalContent (24-206)
  • media (59-69)
src/draive/parameters/model.py (1)
  • DataModel (386-756)
src/draive/lmm/types.py (1)
src/draive/multimodal/content.py (1)
  • MultimodalContent (24-206)
src/draive/lmm/__init__.py (2)
src/draive/lmm/helpers.py (1)
  • lmm_output_decoder (8-35)
src/draive/lmm/types.py (3)
  • LMMOutputDecoder (184-188)
  • LMMOutputInvalid (129-130)
  • LMMOutputLimit (125-126)
🪛 Pylint (3.3.7)
src/draive/gemini/types.py

[convention] 1-1: Missing module docstring

(C0114)


[error] 1-1: Cannot import 'draive.lmm.types' due to 'invalid syntax (draive.lmm.types, line 73)'

(E0001)


[error] 1-1: No name 'types' in module 'draive.lmm'

(E0611)


[convention] 6-6: Missing class docstring

(C0115)

src/draive/lmm/helpers.py

[convention] 1-1: Missing module docstring

(C0114)


[error] 1-1: Cannot import 'draive.lmm.types' due to 'invalid syntax (draive.lmm.types, line 73)'

(E0001)


[error] 1-1: No name 'types' in module 'draive.lmm'

(E0611)


[error] 3-3: Cannot import 'draive.parameters.model' due to 'expected '(' (draive.parameters.model, line 46)'

(E0001)


[error] 3-3: No name 'model' in module 'draive.parameters'

(E0611)


[convention] 8-8: Missing function or method docstring

(C0116)


[refactor] 8-8: Too many return statements (8/6)

(R0911)

src/draive/gemini/lmm.py

[convention] 1-1: Missing module docstring

(C0114)

src/draive/lmm/__init__.py

[convention] 1-1: Missing module docstring

(C0114)

src/draive/gemini/lmm_generation.py

[error] 46-46: Cannot import 'draive.lmm.types' due to 'invalid syntax (draive.lmm.types, line 73)'

(E0001)


[error] 46-46: No name 'types' in module 'draive.lmm'

(E0611)


[refactor] 140-140: Too many branches (17/12)

(R0912)


[refactor] 265-292: Unnecessary "elif" after "return", remove the leading "el" from "elif"

(R1705)


[warning] 626-626: TODO: handle specific tool selection?

(W0511)


[refactor] 328-328: Too many branches (15/12)

(R0912)


[refactor] 523-523: Too many local variables (17/15)

(R0914)


[refactor] 523-523: Too many statements (57/50)

(R0915)

🔇 Additional comments (14)
src/draive/gemini/config.py (1)

25-25: LGTM! Clean addition of new configuration field.

The thinking_budget field follows the established pattern of optional configuration parameters and uses proper Python 3.12+ type union syntax.

src/draive/gemini/types.py (2)

1-1: Import alignment with unified exception hierarchy.

The import of LMMException properly aligns with the refactoring to use a unified exception hierarchy across the LMM modules.


6-7: LGTM! Exception hierarchy properly unified.

Changing GeminiException to inherit from LMMException correctly integrates Gemini-specific errors into the common LMM error handling structure, which aligns with the PR objective of refining error handling.

src/draive/__init__.py (2)

138-139: New exception classes properly imported.

The import of LMMOutputInvalid and LMMOutputLimit correctly exposes the new exception types from the LMM module at the package level.


301-302: Exception classes properly exported in all.

The new exception classes are correctly added to the __all__ list, making them part of the public API as intended.

src/draive/lmm/__init__.py (3)

1-1: Helper function properly imported.

The import of lmm_output_decoder correctly exposes the new output decoding functionality at the package level.


12-14: New types properly imported.

The import of LMMOutputDecoder, LMMOutputInvalid, and LMMOutputLimit correctly exposes the new protocol and exception types at the package level.


51-53: New API elements properly exported.

All new types and the helper function are correctly added to the __all__ list, properly extending the public API surface of the lmm package.

Also applies to: 78-78

src/draive/lmm/types.py (3)

36-38: New exception types properly added to exports.

The new exception classes are correctly added to the __all__ list for proper public API exposure.


125-130: Clean exception class definitions.

The new exception classes LMMOutputLimit and LMMOutputInvalid follow the established pattern of inheriting from LMMException and provide clear separation of error types for output handling scenarios.


183-188: Well-defined output decoder protocol.

The LMMOutputDecoder protocol provides a clean interface for output decoding functionality with proper runtime checking enabled. The protocol signature is clear and follows the multimodal content processing pattern used throughout the codebase.

src/draive/gemini/lmm.py (1)

231-252: Document rationale for disabled safety settings

All safety settings are disabled, which could allow harmful content. Please add a comment explaining why this is necessary, or consider making it configurable.

+# Safety settings are disabled because [explain rationale]
+# Consider making this configurable based on use case requirements
 DISABLED_SAFETY_SETTINGS: list[SafetySettingDict] = [
src/draive/gemini/lmm_generation.py (2)

170-293: Excellent error handling and observability improvements!

The refactored error handling provides clear error messages, detailed logging, and proper handling of edge cases. The safety rating logging and finish reason handling are particularly well done.


688-693: Good implementation of thinking_budget support

The thinking_budget configuration is properly integrated with appropriate conditional logic.

@KaQuMiQ KaQuMiQ force-pushed the feature/gemini_handling branch 4 times, most recently from 9cdef5b to c3688e8 Compare July 1, 2025 14:59
@KaQuMiQ KaQuMiQ force-pushed the feature/gemini_handling branch from c3688e8 to f73af54 Compare July 1, 2025 15:03
@KaQuMiQ KaQuMiQ merged commit adcf77e into main Jul 1, 2025
4 of 5 checks passed
@KaQuMiQ KaQuMiQ deleted the feature/gemini_handling branch July 1, 2025 15:04
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