# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
from typing import TYPE_CHECKING, Any, Literal, Type, Union
if TYPE_CHECKING:
from opentelemetry.util.genai._inference_invocation import ( # pylint: disable=useless-import-alias
LLMInvocation as LLMInvocation, # noqa: PLC0414
)
from opentelemetry.util.genai._invocation import ( # pylint: disable=useless-import-alias
GenAIInvocation as GenAIInvocation, # noqa: PLC0414
)
[docs]class ContentCapturingMode(Enum):
# Do not capture content (default).
NO_CONTENT = 0
# Only capture content in spans.
SPAN_ONLY = 1
# Only capture content in events.
EVENT_ONLY = 2
# Capture content in both spans and events.
SPAN_AND_EVENT = 3
[docs]@dataclass()
class GenericPart:
"""Used for provider-specific message part types that don't match
the standard MessagePart types defined in semantic conventions. Wrap custom
types with GenericPart(value=...) to explicitly opt-in to non-standard types.
This will be removed in a future version when all instrumentations use core types."""
value: Any
type: Literal["generic"] = "generic"
[docs]@dataclass()
class Text:
"""Represents text content sent to or received from the model
This model is specified as part of semconv in `GenAI messages Python models - TextPart
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
"""
content: str
type: Literal["text"] = "text"
[docs]@dataclass()
class Reasoning:
"""Represents reasoning/thinking content received from the model
This model is specified as part of semconv in `GenAI messages Python models - ReasoningPart
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
"""
content: str
type: Literal["reasoning"] = "reasoning"
Modality = Literal["image", "video", "audio"]
[docs]@dataclass()
class Blob:
"""Represents blob binary data sent inline to the model
This model is specified as part of semconv in `GenAI messages Python models - BlobPart
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
"""
mime_type: str | None
modality: Union[Modality, str]
content: bytes
type: Literal["blob"] = "blob"
[docs]@dataclass()
class File:
"""Represents an external referenced file sent to the model by file id
This model is specified as part of semconv in `GenAI messages Python models - FilePart
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
"""
mime_type: str | None
modality: Union[Modality, str]
file_id: str
type: Literal["file"] = "file"
[docs]@dataclass()
class Uri:
"""Represents an external referenced file sent to the model by URI
This model is specified as part of semconv in `GenAI messages Python models - UriPart
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
"""
mime_type: str | None
modality: Union[Modality, str]
uri: str
type: Literal["uri"] = "uri"
ToolDefinition = Union[FunctionToolDefinition, GenericToolDefinition]
MessagePart = Union[
Text,
ToolCallRequest,
ToolCallResponse,
ServerToolCall,
ServerToolCallResponse,
Blob,
File,
Uri,
Reasoning,
GenericPart, # For provider-specific types; prefer standard types above
]
FinishReason = Literal[
"content_filter", "error", "length", "stop", "tool_calls"
]
[docs]@dataclass()
class OutputMessage:
role: str
parts: list[MessagePart]
finish_reason: str | FinishReason
[docs]@dataclass
class Error:
message: str
type: Type[BaseException]
def __getattr__(name: str) -> object:
if name == "GenAIInvocation":
import opentelemetry.util.genai.invocation as _inv # noqa: PLC0415 # pylint: disable=import-outside-toplevel
return _inv.GenAIInvocation
if name == "LLMInvocation":
from opentelemetry.util.genai._inference_invocation import ( # noqa: PLC0415 # pylint: disable=import-outside-toplevel
LLMInvocation,
)
return LLMInvocation
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")