fix(tracing): make OpenTelemetry an optional runtime dependency#30
Merged
Conversation
`import scorecard_ai` previously failed with `ModuleNotFoundError: No module named 'opentelemetry'` unless the optional `[otel]` extra was installed, because the top-level package eagerly imported `lib/wrap_llms.py`, which imported opentelemetry at module load time. This made the documented `pip install scorecard-ai` broken. Move the OpenTelemetry-backed implementation into a private `_wrap_llms_otel` module and turn `wrap_llms` into a thin shim whose `wrap`/`wrap_openai`/`wrap_anthropic` lazily import the implementation on first use. When opentelemetry is missing, calling a wrapper now raises a clear ImportError pointing to `pip install 'scorecard-ai[otel]'` instead of breaking import of the whole SDK.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
otelis declared as an optional extra inpyproject.toml, butimport scorecard_aifailed entirely unless[otel]was installed:The top-level package (
src/scorecard_ai/__init__.py) eagerly importslib/wrap_llms.py, which importedopentelemetryat module load time. So the documentedpip install scorecard-aiproduced a broken SDK — the otel extra was effectively mandatory just to use the core API client.Reproduced (with opentelemetry absent):
Fix
Make OpenTelemetry genuinely optional by deferring its import to first use of the tracing wrappers:
lib/_wrap_llms_otel.py(new) — the existing OpenTelemetry-backed implementation, moved verbatim. Still imports opentelemetry at the top.lib/wrap_llms.py— now a thin shim with no top-level otel import.wrap/wrap_openai/wrap_anthropiclazily import the implementation on first call. If opentelemetry is missing, they raise a clearImportErrorpointing topip install 'scorecard-ai[otel]'instead of breaking import of the whole SDK. The type-onlyWrapConfigstays underTYPE_CHECKING.All three public import paths are preserved:
scorecard_ai.wrap,scorecard_ai.lib.wrap, andscorecard_ai.lib.wrap_llms.wrap.Verification
import scorecard_aisucceeds with opentelemetry blocked; coreScorecardclient constructs and works.wrap()without otel raises a helpfulImportError(not an opaqueModuleNotFoundError).wrap()end-to-end still returns the_LLMClientWrapper; aliases preserved (wrap_openai is wrap).ruff checkandruff formatpass on both files.Notes
@opentelemetry/*stay requireddependenciesthere, so it already imports fine.[otel]install for tracing is already documented inexamples/README.md, and the new error message points there too.