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
Expand Up @@ -36,9 +36,8 @@
class AgentInvocation(GenAIInvocation):
"""Represents a single agent invocation (invoke_agent span).

Use handler.start_invoke_local_agent() / handler.start_invoke_remote_agent()
or the handler.invoke_local_agent() / handler.invoke_remote_agent() context
managers rather than constructing this directly.
Use handler.invoke_local_agent() or handler.invoke_remote_agent()
rather than constructing this directly.

Reference:
Client span: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-agent-spans.md#invoke-agent-client-span
Expand All @@ -59,7 +58,7 @@ def __init__(
server_port: int | None = None,
agent_name: str | None = None,
) -> None:
"""Use handler.start_invoke_local_agent() or handler.start_invoke_remote_agent() instead of calling this directly."""
"""Use handler.invoke_local_agent() or handler.invoke_remote_agent() instead of calling this directly."""
_operation_name = GenAI.GenAiOperationNameValues.INVOKE_AGENT.value
super().__init__(
tracer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
class EmbeddingInvocation(GenAIInvocation):
"""Represents a single embedding model invocation.

Use handler.start_embedding(provider) or the handler.embedding(provider)
context manager rather than constructing this directly.
Use handler.embedding(provider) rather than constructing this directly.
"""

def __init__(
Expand All @@ -36,7 +35,7 @@ def __init__(
server_address: str | None = None,
server_port: int | None = None,
) -> None:
"""Use handler.start_embedding(provider) or handler.embedding(provider) instead of calling this directly."""
"""Use handler.embedding(provider) rather than calling this directly."""
_operation_name = GenAI.GenAiOperationNameValues.EMBEDDINGS.value
super().__init__(
tracer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
class InferenceInvocation(GenAIInvocation):
"""Represents a single LLM chat/completion call.

Use handler.start_inference(provider) or the handler.inference(provider)
context manager rather than constructing this directly.
Use handler.inference(provider) rather than constructing this directly.
"""

def __init__(
Expand All @@ -57,7 +56,7 @@ def __init__(
operation_name = (
operation_name or GenAI.GenAiOperationNameValues.CHAT.value
)
"""Use handler.start_inference(provider) or handler.inference(provider) instead of calling this directly."""
"""Use handler.inference(provider) rather than calling this directly."""
super().__init__(
tracer,
metrics_recorder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import timeit
from abc import ABC, abstractmethod
from contextlib import contextmanager
from contextvars import Token
from dataclasses import asdict
from typing import TYPE_CHECKING, Any, Generator, Sequence
from types import TracebackType
from typing import TYPE_CHECKING, Any, Sequence

from typing_extensions import Self, TypeAlias
from typing_extensions import TypeAlias

from opentelemetry._logs import Logger, LogRecord
from opentelemetry.context import Context, attach, detach
Expand Down Expand Up @@ -165,13 +165,17 @@ def fail(self, error: Error | BaseException) -> None:
error = Error(type=type(error), message=str(error))
self._finish(error)

@contextmanager
def _managed(self) -> Generator[Self, None, None]:
"""Context manager that calls stop() on success or fail() on exception."""
try:
yield self
except Exception as exc:
self.fail(exc)
def __enter__(self):
return self

def __exit__(
self,
type_: type[BaseException] | None,
value: BaseException | None,
traceback: TracebackType | None,
) -> None:
if value:
self.fail(value)
raise
self.stop()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
tool_call_id: str | None = None,
tool_type: str | None = None,
tool_description: str | None = None,
tool_result: Any | None = None,
) -> None:
"""Use handler.start_tool(name) or handler.tool(name) instead of calling this directly."""
_operation_name = GenAI.GenAiOperationNameValues.EXECUTE_TOOL.value
Expand All @@ -59,11 +60,11 @@ def __init__(
span_name=f"{_operation_name} {name}" if name else _operation_name,
)
self.name = name
self.tool_result = tool_result
self.arguments = arguments
self.tool_call_id = tool_call_id
self.tool_type = tool_type
self.tool_description = tool_description
self.tool_result: Any = None
self._start(self._get_base_attributes())

def _get_base_attributes(self) -> dict[str, Any]:
Expand All @@ -73,6 +74,8 @@ def _get_base_attributes(self) -> dict[str, Any]:
(GenAI.GEN_AI_TOOL_CALL_ID, self.tool_call_id),
(GenAI.GEN_AI_TOOL_TYPE, self.tool_type),
(GenAI.GEN_AI_TOOL_DESCRIPTION, self.tool_description),
(GenAI.GEN_AI_TOOL_CALL_ARGUMENTS, self.arguments),
(GenAI.GEN_AI_TOOL_CALL_RESULT, self.tool_result),
Comment on lines +77 to +78
)
return {
GenAI.GEN_AI_OPERATION_NAME: self._operation_name,
Expand All @@ -95,6 +98,7 @@ def _apply_finish(self, error: Error | None = None) -> None:
(GenAI.GEN_AI_TOOL_TYPE, self.tool_type),
(GenAI.GEN_AI_TOOL_DESCRIPTION, self.tool_description),
(GenAI.GEN_AI_TOOL_CALL_ARGUMENTS, self.arguments),
(GenAI.GEN_AI_TOOL_CALL_RESULT, self.tool_result),
)
attributes: dict[str, Any] = {
GenAI.GEN_AI_OPERATION_NAME: self._operation_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class WorkflowInvocation(GenAIInvocation):
and retrieval invocations). A workflow groups multiple operations together,
accepting input(s) and producing final output(s).

Use handler.start_workflow(name) or the handler.workflow(name) context
manager rather than constructing this directly.
Use handler.workflow(name) rather than constructing this directly.
"""

def __init__(
Expand All @@ -44,7 +43,7 @@ def __init__(
completion_hook: CompletionHook,
name: str | None,
) -> None:
"""Use handler.start_workflow(name) or handler.workflow(name) instead of calling this directly."""
"""Use handler.workflow(name) rather than calling this directly."""
_operation_name = "invoke_workflow"
super().__init__(
tracer,
Expand Down
Loading
Loading