Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import asdict
from enum import Enum
from typing import Union
from typing import Union, TYPE_CHECKING

from opentelemetry._events import Event, EventLogger
from opentelemetry.instrumentation.replicate.event_models import (
Expand All @@ -16,7 +16,8 @@
gen_ai_attributes as GenAIAttributes,
)

from replicate.prediction import Prediction
if TYPE_CHECKING:
from replicate.prediction import Prediction


class Roles(Enum):
Expand All @@ -35,7 +36,7 @@ class Roles(Enum):

@dont_throw
def emit_choice_events(
response: Union[str, list, Prediction], event_logger: Union[EventLogger, None]
response: Union[str, list, "Prediction"], event_logger: Union[EventLogger, None]
):
# Handle replicate.run responses
if isinstance(response, list):
Expand All @@ -47,7 +48,7 @@ def emit_choice_events(
event_logger,
)
# Handle replicate.predictions.create responses
elif isinstance(response, Prediction):
elif isinstance(response, "Prediction"):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Bug: isinstance() cannot take a string as the type argument.

isinstance(response, "Prediction") raises TypeError at runtime. Since we intentionally avoid importing Prediction at module import time, use a duck-typed and module-aware check instead.

Apply this diff:

-    elif isinstance(response, "Prediction"):
+    elif hasattr(response, "output") and getattr(response.__class__, "__module__", "").startswith("replicate."):

This preserves the no-import-at-import-time behavior while correctly detecting Replicate Prediction-like objects at runtime.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
elif isinstance(response, "Prediction"):
elif hasattr(response, "output") and getattr(response.__class__, "__module__", "").startswith("replicate."):
🤖 Prompt for AI Agents
In
packages/opentelemetry-instrumentation-replicate/opentelemetry/instrumentation/replicate/event_emitter.py
around line 51, the code uses isinstance(response, "Prediction") which raises
TypeError because isinstance cannot take a string; replace this with a
duck-typed and module-aware runtime check that does not import Prediction at
module load: inspect the response's class name and module (e.g. class.__name__
== "Prediction" and class.__module__ startswith the replicate package/module)
and/or check for a small set of Prediction-specific attributes/methods to
reliably identify Prediction-like objects at runtime, then branch accordingly.

emit_event(
ChoiceEvent(
index=0, message={"content": response.output, "role": "assistant"}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys


def test_import():
import opentelemetry.instrumentation.replicate # noqa: F401

assert "replicate" not in sys.modules