Skip to content

Adding scope and span_data doc strings #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
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
4 changes: 4 additions & 0 deletions src/agents/tracing/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@


class Scope:
"""
Manages the current span and trace in the context.
"""

@classmethod
def get_current_span(cls) -> "Span[Any] | None":
return _current_span.get()
Expand Down
60 changes: 60 additions & 0 deletions src/agents/tracing/span_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@


class SpanData(abc.ABC):
"""
Represents span data in the trace.
"""

@abc.abstractmethod
def export(self) -> dict[str, Any]:
"""Export the span data as a dictionary."""
pass

@property
@abc.abstractmethod
def type(self) -> str:
"""Return the type of the span."""
pass


class AgentSpanData(SpanData):
"""
Represents an Agent Span in the trace.
Includes name, handoffs, tools, and output type.
"""

__slots__ = ("name", "handoffs", "tools", "output_type")

def __init__(
Expand Down Expand Up @@ -49,6 +60,11 @@ def export(self) -> dict[str, Any]:


class FunctionSpanData(SpanData):
"""
Represents a Function Span in the trace.
Includes input, output and MCP data (if applicable).
"""

__slots__ = ("name", "input", "output", "mcp_data")

def __init__(
Expand Down Expand Up @@ -78,6 +94,11 @@ def export(self) -> dict[str, Any]:


class GenerationSpanData(SpanData):
"""
Represents a Generation Span in the trace.
Includes input, output, model, model configuration, and usage.
"""

__slots__ = (
"input",
"output",
Expand Down Expand Up @@ -116,6 +137,11 @@ def export(self) -> dict[str, Any]:


class ResponseSpanData(SpanData):
"""
Represents a Response Span in the trace.
Includes response and input.
"""

__slots__ = ("response", "input")

def __init__(
Expand All @@ -140,6 +166,11 @@ def export(self) -> dict[str, Any]:


class HandoffSpanData(SpanData):
"""
Represents a Handoff Span in the trace.
Includes source and desitnation agents.
"""

__slots__ = ("from_agent", "to_agent")

def __init__(self, from_agent: str | None, to_agent: str | None):
Expand All @@ -159,6 +190,11 @@ def export(self) -> dict[str, Any]:


class CustomSpanData(SpanData):
"""
Represents a Custom Span in the trace.
Includes name and data property bag.
"""

__slots__ = ("name", "data")

def __init__(self, name: str, data: dict[str, Any]):
Expand All @@ -178,6 +214,11 @@ def export(self) -> dict[str, Any]:


class GuardrailSpanData(SpanData):
"""
Represents a Guardrail Span in the trace.
Includes name and triggered status.
"""

__slots__ = ("name", "triggered")

def __init__(self, name: str, triggered: bool = False):
Expand All @@ -197,6 +238,11 @@ def export(self) -> dict[str, Any]:


class TranscriptionSpanData(SpanData):
"""
Represents a Transcription Span in the trace.
Includes input, output, model, and model configuration.
"""

__slots__ = (
"input",
"output",
Expand Down Expand Up @@ -236,6 +282,11 @@ def export(self) -> dict[str, Any]:


class SpeechSpanData(SpanData):
"""
Represents a Speech Span in the trace.
Includes input, output, model, model configuration, and first content timestamp.
"""

__slots__ = ("input", "output", "model", "model_config", "first_content_at")

def __init__(
Expand Down Expand Up @@ -273,6 +324,10 @@ def export(self) -> dict[str, Any]:


class SpeechGroupSpanData(SpanData):
"""
Represents a Speech Group Span in the trace.
"""

__slots__ = "input"

def __init__(
Expand All @@ -293,6 +348,11 @@ def export(self) -> dict[str, Any]:


class MCPListToolsSpanData(SpanData):
"""
Represents an MCP List Tools Span in the trace.
Includes server and result.
"""

__slots__ = (
"server",
"result",
Expand Down