openllmetry icon indicating copy to clipboard operation
openllmetry copied to clipboard

fix(google-generativeai): defer google-generativeai to allow omission from runtime dependencies

Open timkpaine opened this issue 5 months ago β€’ 2 comments

xref: https://github.com/conda-forge/opentelemetry-instrumentation-google-generativeai-feedstock/pull/23

https://github.com/traceloop/openllmetry/blob/645426360910ce56d00894fc9c7feb47d5230953/packages/opentelemetry-instrumentation-google-generativeai/opentelemetry/instrumentation/google_generativeai/init.py#L7

import: 'opentelemetry.instrumentation.google_generativeai'
Traceback (most recent call last):
  File "/home/conda/feedstock_root/build_artifacts/opentelemetry-instrumentation-google-generativeai_1752459194594/test_tmp/run_test.py", line 2, in <module>
    import opentelemetry.instrumentation.google_generativeai
  File "/home/conda/feedstock_root/build_artifacts/opentelemetry-instrumentation-google-generativeai_1752459194594/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeh/lib/python3.9/site-packages/opentelemetry/instrumentation/google_generativeai/__init__.py", line 7, in <module>
    from google.generativeai.types.generation_types import GenerateContentResponse
ModuleNotFoundError: No module named 'google'
  • [x] I have added tests that cover my changes.
  • [ ] If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • [x] PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • [ ] (If applicable) I have updated the documentation accordingly.

[!IMPORTANT] Move google-generativeai to runtime dependencies in pyproject.toml to fix import error.

  • Dependencies:
    • Move google-generativeai from [tool.poetry.group.dev.dependencies] to [tool.poetry.dependencies] in pyproject.toml to fix ModuleNotFoundError.
  • Error Fix:
    • Resolves ModuleNotFoundError for google.generativeai.types.generation_types in __init__.py.

This description was created by Ellipsis for 136f51fb3c5779c41546ad5f76de627cb3e2afb4. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • Bug Fixes

    • Prevents eager importing of heavy Google Generative AI dependencies during initialization, reducing startup overhead and avoiding import-time errors when those packages aren’t installed.
    • Uses type-only references for external types so optional packages aren’t required at runtime.
  • Tests

    • Added a test to verify importing the instrumentation does not load Google Generative AI modules, guarding against regressions.

timkpaine avatar Jul 14 '25 02:07 timkpaine

Walkthrough

Introduced TYPE_CHECKING-guarded import for GenerateContentResponse and replaced runtime type annotations with forward-references; added a test ensuring importing the instrumentation package does not eagerly load google.genai.

Changes

Cohort / File(s) Summary
Type-hint import guard
packages/opentelemetry-instrumentation-google-generativeai/opentelemetry/instrumentation/google_generativeai/__init__.py
Added TYPE_CHECKING to typing imports, moved GenerateContentResponse import into an if TYPE_CHECKING: block, and replaced runtime type references with forward-referenced annotations ("GenerateContentResponse"). No runtime logic changes.
Import behavior test
packages/opentelemetry-instrumentation-google-generativeai/tests/test_generate_content.py
Added test_imports() which imports the instrumentation package and asserts "google.genai" is not present in sys.modules to ensure no eager import of heavy dependencies.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • traceloop/openllmetry#3289 β€” Modifies how GenerateContentResponse is imported/used in the google_generativeai instrumentation (overlaps with TYPE_CHECKING/forward-ref changes).
  • traceloop/openllmetry#3282 β€” Adjusts import/handling of GenerateContentResponse in the same module; closely related to these import/annotation edits.

Suggested reviewers

  • galkleinman

Poem

A rabbit tucks types out of sight,
Softly guards imports till runtime's right.
Tests hop in, check modules clear,
Lightweight imports bring a cheer.
πŸ₯•

[!TIP]

πŸ”Œ Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


πŸ“œ Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

πŸ’‘ Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 08860f6f1a7fafe2e66cec2b1f4db362eb8f2c18 and 490213fa32a95aece2722f34594560c1a7f93da9.

πŸ“’ Files selected for processing (2)
  • packages/opentelemetry-instrumentation-google-generativeai/opentelemetry/instrumentation/google_generativeai/__init__.py (4 hunks)
  • packages/opentelemetry-instrumentation-google-generativeai/tests/test_generate_content.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/opentelemetry-instrumentation-google-generativeai/tests/test_generate_content.py
  • packages/opentelemetry-instrumentation-google-generativeai/opentelemetry/instrumentation/google_generativeai/init.py
✨ Finishing Touches
  • [ ] πŸ“ Generate Docstrings
πŸ§ͺ Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

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

CodeRabbit Commands (Invoked using PR/Issue comments)

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

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

coderabbitai[bot] avatar Aug 14 '25 21:08 coderabbitai[bot]

@timkpaine can you take a look? Looks like test and lint are failing

nirga avatar Aug 21 '25 19:08 nirga