Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix: Agno workflow arun() method being wrapped with coroutine. (#917)

Patcher0e6bd76e7ad9
diff --git a/sdk/python/.pylintrc b/sdk/python/.pylintrc
index 16b475e2..f8a2d083 100644
--- a/sdk/python/.pylintrc
+++ b/sdk/python/.pylintrc
@@ -36,6 +36,7 @@ disable=
too-many-return-statements,
too-many-function-args,
unnecessary-pass,
- invalid-name
+ invalid-name,
+ import-error
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 7596d9df..66b66aa6 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.35.8"
+version = "1.35.9"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
license = "Apache-2.0"
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index b241cafe..7543c37d 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -427,9 +427,14 @@ def common_span_attributes(
scope._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, gen_ai_system)
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
+ if request_model:
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, request_model
+ )
if response_model:
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, response_model)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_MODEL, response_model
+ )
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
diff --git a/sdk/python/src/openlit/instrumentation/agno/__init__.py b/sdk/python/src/openlit/instrumentation/agno/__init__.py
index ac7fb80d..ebce1070 100644
--- a/sdk/python/src/openlit/instrumentation/agno/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/agno/__init__.py
@@ -6,7 +6,8 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.agno.utils import (
resolve_agno_knowledge_target,
- resolve_agno_memory_target)
+ resolve_agno_memory_target,
+)
from openlit.instrumentation.agno.agno import (
agent_run_wrap,
agent_continue_run_wrap,
@@ -71,6 +72,7 @@ class AgnoInstrumentor(BaseInstrumentor):
"""Initialize the AgnoInstrumentor."""
super().__init__()
self._original_thread_pool_submit = None
+ self._original_runtime_detach = None
def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
@@ -91,8 +93,8 @@ class AgnoInstrumentor(BaseInstrumentor):
except importlib.metadata.PackageNotFoundError:
version = "unknown"
- # CRITICAL: Patch ThreadPoolExecutor globally to preserve OpenTelemetry context
- self._patch_thread_pool_executor()
+ # Patch OpenTelemetry's context detach to suppress benign errors in async/threaded scenarios
+ self._patch_otel_context_detach()
# Workflow Operations (Always Instrumented)
wrap_function_wrapper(
@@ -243,7 +245,6 @@ class AgnoInstrumentor(BaseInstrumentor):
memory_module, memory_class = resolve_agno_memory_target()
if memory_module is not None:
-
# Wrap memory add/search exactly once using the dedicated wrappers to avoid
# duplicate spans and performance overhead.
wrap_function_wrapper(
@@ -336,7 +337,6 @@ class AgnoInstrumentor(BaseInstrumentor):
# agno 2.x versions do not have load method
if hasattr(knowledge_class, "load"):
-
wrap_function_wrapper(
knowledge_module,
f"{knowledge_class}.load",
@@ -427,12 +427,10 @@ class AgnoInstrumentor(BaseInstrumentor):
),
)
-
-
# Team Operations
try:
- from agno.team.team import Team # pylint: disable=import-error
+ from agno.team.team import Team # pylint: disable=import-error
except ImportError:
Team = None
# agno 2.x versions do use Team._arun_stream and Team._arun methods
@@ -611,26 +609,45 @@ class AgnoInstrumentor(BaseInstrumentor):
# CRITICAL: Parallel execution operations for context preservation
# Note: Parallel class doesn't have a run method, context is preserved via Model.run_function_calls
- wrap_function_wrapper(
- "agno.team.team",
- "Team._make_memories_and_summaries",
- parallel_execution_wrap(
- "team._make_memories_and_summaries",
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- ),
- )
+ # NOTE: Team._make_memories_and_summaries method was removed in recent Agno versions
+ # Only Team._make_memories exists now. If parallel execution instrumentation is needed,
+ # it should be added for Team._make_memories instead.
# NOTE: Removed duplicate reasoning tool instrumentation
# The bridge spans from Model.run_function_call provide sufficient tracing
# without creating redundant spans for ReasoningTools.think/analyze
+ def _patch_otel_context_detach(self):
+ """
+ Patch OpenTelemetry's context detach to suppress benign cross-context errors.
+
+ Agno uses ThreadPoolExecutor internally for tool execution, which can cause
+ context tokens to be detached in a different context than where they were created.
+ This is benign but OTel logs it as an error. We suppress the logging for these cases.
+ """
+ import logging
+ from opentelemetry.context import _RUNTIME_CONTEXT
+
+ # Suppress the specific logger that logs "Failed to detach context"
+ otel_context_logger = logging.getLogger("opentelemetry.context")
+ otel_context_logger.setLevel(logging.CRITICAL)
+
+ # Also patch the runtime context's detach to not raise for cross-context errors
+ original_detach = _RUNTIME_CONTEXT.detach
+
+ def patched_detach(token):
+ """Detach context token, suppressing cross-context ValueError"""
+ try:
+ original_detach(token)
+ except ValueError as e:
+ # Silently ignore "was created in a different Context" errors
+ # These occur when Agno's ThreadPoolExecutor runs tools in different threads
+ if "was created in a different Context" not in str(e):
+ raise
+
+ _RUNTIME_CONTEXT.detach = patched_detach
+ self._original_runtime_detach = original_detach
+
def _patch_thread_pool_executor(self):
"""
CRITICAL: Patch ThreadPoolExecutor.submit globally to preserve OpenTelemetry context.
@@ -648,12 +665,26 @@ class AgnoInstrumentor(BaseInstrumentor):
current_context = context_api.get_current()
def context_wrapper(*wrapper_args, **wrapper_kwargs):
- # Restore context in the thread
- token = context_api.attach(current_context)
+ # OpenTelemetry context management for threaded execution
+ # Note: In async/threaded scenarios, the context token may become invalid
+ # if the execution context changes. We handle this gracefully.
+ token = None
+ try:
+ token = context_api.attach(current_context)
+ except Exception:
+ # If attach fails, continue without context propagation
+ pass
+
try:
return func(*wrapper_args, **wrapper_kwargs)
finally:
- context_api.detach(token)
+ if token is not None:
+ try:
+ context_api.detach(token)
+ except Exception:
+ # Silently ignore detach errors - can happen when context changes
+ # between attach and detach in async/threaded scenarios
+ pass
return original_submit(self, context_wrapper, *args, **kwargs)
diff --git a/sdk/python/src/openlit/instrumentation/agno/async_agno.py b/sdk/python/src/openlit/instrumentation/agno/async_agno.py
index 0686abf4..59a7828c 100644
--- a/sdk/python/src/openlit/instrumentation/agno/async_agno.py
+++ b/sdk/python/src/openlit/instrumentation/agno/async_agno.py
@@ -5,7 +5,6 @@ Module for monitoring Agno agent framework async operations.
import logging
import time
from opentelemetry.trace import Status, StatusCode, SpanKind
-from opentelemetry import context as context_api
from openlit.__helpers import (
handle_exception,
)
@@ -41,10 +40,6 @@ def async_agent_run_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
# Extract agent name for span naming with fallback to agent_id
agent_name = (
getattr(instance, "name", None)
@@ -105,10 +100,6 @@ def async_agent_continue_run_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
agent_name = getattr(instance, "name", "unknown")
span_name = f"continue {agent_name}"
@@ -163,10 +154,6 @@ def async_model_run_function_call_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
# Extract function call information for span naming
function_call = args[0] if args else kwargs.get("function_call", None)
function_name = None
@@ -237,15 +224,7 @@ def async_agent_run_stream_wrap(
Wrap Agno Agent._arun_stream async generator method.
"""
-
async def wrapper(wrapped, instance, args, kwargs):
-
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- async for response in wrapped(*args, **kwargs):
- yield response
- return
-
# Extract agent name for span naming with fallback to agent_id
agent_name = (
getattr(instance, "name", None)
@@ -256,6 +235,7 @@ def async_agent_run_stream_wrap(
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
+ final_response = None
try:
# agno 2.x: when `yield_run_response=True`, the final `RunOutput` is yielded
# rather than stored on `instance.run_response`
@@ -264,9 +244,8 @@ def async_agent_run_stream_wrap(
except Exception: # noqa: WPS429
RunOutput = None # type: ignore # pylint: disable=invalid-name
yield_run_response = kwargs.get("yield_run_response", None)
- final_response = None
new_kwargs = dict(kwargs)
- new_kwargs['yield_run_response'] = True
+ new_kwargs["yield_run_response"] = True
async for response in wrapped(*args, **new_kwargs):
if RunOutput and isinstance(response, RunOutput):
final_response = response
@@ -277,37 +256,44 @@ def async_agent_run_stream_wrap(
if not RunOutput:
# Get the final response after iteration completes
final_response = getattr(instance, "run_response", None)
+ except GeneratorExit:
+ # Generator was closed early by consumer - this is normal
+ # Don't log as error, just mark span complete with what we have
+ pass
except Exception as e:
handle_exception(span, e)
logger.error("Error in async agent._arun_stream: %s", e)
raise
+ finally:
+ # Always process telemetry in finally block to ensure it runs
+ try:
+ # Process request using utils function with ALL attributes from semcov
+ process_agent_request(
+ span,
+ instance,
+ args,
+ kwargs,
+ final_response,
+ start_time,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
- try:
- # Process request using utils function with ALL attributes from semcov
- process_agent_request(
- span,
- instance,
- args,
- kwargs,
- final_response,
- start_time,
- pricing_info,
- environment,
- application_name,
- metrics,
- capture_message_content,
- disable_metrics,
- version,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
- )
-
- # Mark span as successful
- span.set_status(Status(StatusCode.OK))
+ # Mark span as successful
+ span.set_status(Status(StatusCode.OK))
- except Exception as e:
- # Handle instrumentation exceptions - log but don't raise
- handle_exception(span, e)
- logger.error("Error in async agent._arun_stream trace creation: %s", e)
+ except Exception as e:
+ # Handle instrumentation exceptions - log but don't raise
+ handle_exception(span, e)
+ logger.error(
+ "Error in async agent._arun_stream trace creation: %s", e
+ )
return wrapper
@@ -329,12 +315,6 @@ def async_model_run_function_calls_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- async for item in wrapped(*args, **kwargs):
- yield item
- return
-
# Extract function calls information for span naming
function_calls = args[0] if args else []
function_names = []
@@ -350,31 +330,17 @@ def async_model_run_function_calls_wrap(
if len(function_names) > 3:
span_name += f" (+{len(function_names) - 3} more)"
- # CRITICAL: Capture current OpenTelemetry context to ensure proper nesting
- current_context = context_api.get_current()
-
- with tracer.start_as_current_span(
- span_name, kind=SpanKind.INTERNAL, context=current_context
- ) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.INTERNAL) as span:
start_time = time.time()
- # CRITICAL: Create the context with this span active for nested calls
- span_context = context_api.set_value("current_span", span, current_context)
try:
- # CRITICAL: Maintain span context across async iteration
- # We need to collect all items while maintaining context, then yield them
- items = []
- token = context_api.attach(span_context)
- try:
- async for item in wrapped(*args, **kwargs):
- items.append(item)
- finally:
- context_api.detach(token)
-
- # Now yield all collected items (span is still active in the with block)
- for item in items:
+ # Stream items directly - OpenTelemetry's context manager handles context automatically
+ async for item in wrapped(*args, **kwargs):
yield item
+ except GeneratorExit:
+ # Generator was closed early by consumer - this is normal
+ pass
except Exception as e:
handle_exception(span, e)
logger.error(
@@ -429,31 +395,15 @@ def async_function_entrypoint_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
# Extract function information for span naming
function_name = getattr(instance, "name", None) or getattr(
instance, "__name__", "unknown_function"
)
span_name = f"tool {function_name}"
- # CRITICAL: Capture current OpenTelemetry context to ensure proper nesting
- current_context = context_api.get_current()
-
- with tracer.start_as_current_span(
- span_name, kind=SpanKind.INTERNAL, context=current_context
- ) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.INTERNAL) as span:
start_time = time.time()
- # CRITICAL: Set the span context as current for nested calls
- token = context_api.attach(
- context_api.set_value("current_span", span, current_context)
- )
- try:
- result = await wrapped(*args, **kwargs)
- finally:
- context_api.detach(token)
+ result = await wrapped(*args, **kwargs)
try:
# Process request using utils function with ALL attributes from semcov
@@ -502,10 +452,6 @@ def async_function_call_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
# Extract function information
function_name = getattr(
instance, "name", getattr(instance, "__name__", "unknown_function")
@@ -564,10 +510,6 @@ def async_memory_add_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
span_name = "memory add"
with tracer.start_as_current_span(span_name, kind=SpanKind.INTERNAL) as span:
@@ -620,10 +562,6 @@ def async_memory_search_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
span_name = "memory search"
with tracer.start_as_current_span(span_name, kind=SpanKind.INTERNAL) as span:
@@ -677,10 +615,6 @@ def async_vectordb_search_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
span_name = "vectordb search"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -734,10 +668,6 @@ def async_knowledge_search_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
span_name = "knowledge search"
with tracer.start_as_current_span(span_name, kind=SpanKind.INTERNAL) as span:
@@ -787,46 +717,90 @@ def async_workflow_run_wrap(
disable_metrics,
):
"""
- Wrap Agno Workflow async run method for workflow execution tracing using process functions only.
+ Wrap Agno Workflow async run method for workflow execution tracing.
+
+ In Agno 2.2+, workflow.arun() returns an async iterator instead of a coroutine.
+ This wrapper handles both patterns for backward compatibility.
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
workflow_name = getattr(instance, "name", "unknown_workflow")
span_name = f"workflow {workflow_name}"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
- result = await wrapped(*args, **kwargs)
- try:
- # Process request using utils function with ALL attributes from semcov
- process_workflow_request(
- span,
- instance,
- args,
- kwargs,
- result,
- start_time,
- pricing_info,
- environment,
- application_name,
- metrics,
- capture_message_content,
- disable_metrics,
- version,
- )
+ # Call the wrapped function to get the result
+ result = wrapped(*args, **kwargs)
- span.set_status(Status(StatusCode.OK))
+ # Check if result is an async iterator (Agno 2.2+)
+ if hasattr(result, "__aiter__"):
+ # Handle async iterator - stream events and collect final response
+ try:
+ final_response = None
+ async for event in result:
+ final_response = event
+ yield event
+
+ # Process the final response for telemetry
+ try:
+ process_workflow_request(
+ span,
+ instance,
+ args,
+ kwargs,
+ final_response,
+ start_time,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
+ span.set_status(Status(StatusCode.OK))
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error(
+ "Error in async workflow run trace creation: %s", e
+ )
- except Exception as e:
- handle_exception(span, e)
- logger.error("Error in async workflow run trace creation: %s", e)
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error("Error in async workflow run iteration: %s", e)
+ raise
+ else:
+ # Legacy coroutine pattern (pre-Agno 2.2)
+ # Await the result and yield it as a single item
+ try:
+ response = await result
- return result
+ # Process request using utils function with ALL attributes from semcov
+ process_workflow_request(
+ span,
+ instance,
+ args,
+ kwargs,
+ response,
+ start_time,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
+ span.set_status(Status(StatusCode.OK))
+
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error("Error in async workflow run trace creation: %s", e)
+ raise
+
+ # Yield the single response to maintain generator contract
+ yield response
return wrapper
@@ -848,10 +822,6 @@ def async_team_run_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
team_name = getattr(instance, "name", "unknown_team")
span_name = f"team {team_name}"
@@ -904,11 +874,6 @@ def async_team_run_stream_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- async for item in wrapped(*args, **kwargs):
- yield item
- return
-
team_name = getattr(instance, "name", "unknown_team")
span_name = f"team {team_name}"
@@ -916,7 +881,6 @@ def async_team_run_stream_wrap(
start_time = time.time()
final_response = None
-
try:
# agno 2.x: when `yield_run_response=True`, the final `TeamRunOutput` is yielded
# rather than stored on `instance.run_response`
@@ -926,9 +890,8 @@ def async_team_run_stream_wrap(
TeamRunOutput = None # type: ignore # pylint: disable=invalid-name
yield_run_response = kwargs.get("yield_run_response", None)
- final_response = None
new_kwargs = dict(kwargs)
- new_kwargs['yield_run_response'] = True
+ new_kwargs["yield_run_response"] = True
async for response in wrapped(*args, **new_kwargs):
if TeamRunOutput and isinstance(response, TeamRunOutput):
final_response = response
@@ -939,31 +902,36 @@ def async_team_run_stream_wrap(
if not TeamRunOutput:
# Get the final response after iteration completes
final_response = getattr(instance, "run_response", None)
+ except GeneratorExit:
+ # Generator was closed early by consumer - this is normal
+ # Don't log as error, just mark span complete with what we have
+ pass
except Exception as e:
handle_exception(span, e)
logger.error("Error in team stream run: %s", e)
raise
-
- try:
- process_team_request(
- span,
- instance,
- args,
- kwargs,
- final_response,
- start_time,
- pricing_info,
- environment,
- application_name,
- metrics,
- capture_message_content,
- disable_metrics,
- version,
- )
- span.set_status(Status(StatusCode.OK))
- except Exception as e:
- handle_exception(span, e)
- logger.error("Error creating team stream trace: %s", e)
+ finally:
+ # Always process telemetry in finally block to ensure it runs
+ try:
+ process_team_request(
+ span,
+ instance,
+ args,
+ kwargs,
+ final_response,
+ start_time,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
+ span.set_status(Status(StatusCode.OK))
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error("Error creating team stream trace: %s", e)
return wrapper
@@ -984,10 +952,6 @@ def async_agent_add_tool_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
agent_name = getattr(instance, "name", "unknown")
span_name = f"agent {agent_name}"
@@ -1041,10 +1005,6 @@ def async_session_memory_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
# Extract method name from endpoint
method_name = gen_ai_endpoint.split(".")[-1]
span_name = f"memory {method_name.replace('get_', '').replace('_', ' ')}"
@@ -1102,30 +1062,13 @@ def async_function_execute_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
# Extract function information
function_name = getattr(instance.function, "name", "unknown_function")
span_name = f"tool {function_name}"
- # CRITICAL: Capture current OpenTelemetry context to ensure proper nesting
- current_context = context_api.get_current()
-
- with tracer.start_as_current_span(
- span_name, kind=SpanKind.INTERNAL, context=current_context
- ) as span:
- # Execute async tool with timing
+ with tracer.start_as_current_span(span_name, kind=SpanKind.INTERNAL) as span:
start_time = time.time()
- # CRITICAL: Set the span context as current for nested calls
- token = context_api.attach(
- context_api.set_value("current_span", span, current_context)
- )
- try:
- result = await wrapped(*args, **kwargs)
- finally:
- context_api.detach(token)
+ result = await wrapped(*args, **kwargs)
try:
# Process request using utils function with ALL attributes from semcov
@@ -1173,10 +1116,6 @@ def async_toolkit_run_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
# Extract toolkit information
toolkit_name = getattr(
instance,
@@ -1236,10 +1175,6 @@ def async_reasoning_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
agent_name = getattr(instance, "name", "unknown_agent")
span_name = f"reasoning {agent_name}"
@@ -1294,10 +1229,6 @@ def async_vectordb_upsert_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
span_name = "vectordb upsert"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -1351,10 +1282,6 @@ def async_knowledge_add_wrap(
"""
async def wrapper(wrapped, instance, args, kwargs):
- # CRITICAL: Suppression check to prevent recursive instrumentation
- if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
- return await wrapped(*args, **kwargs)
-
span_name = "knowledge add"
with tracer.start_as_current_span(span_name, kind=SpanKind.INTERNAL) as span:
diff --git a/sdk/python/src/openlit/instrumentation/agno/utils.py b/sdk/python/src/openlit/instrumentation/agno/utils.py
index 27d60da5..adbd4c19 100644
--- a/sdk/python/src/openlit/instrumentation/agno/utils.py
+++ b/sdk/python/src/openlit/instrumentation/agno/utils.py
@@ -151,14 +151,15 @@ def process_agent_request(
)
# agno 2.x versions agent.run use input instead of args[0]
- if kwargs and kwargs.get('input', None) and capture_message_content:
+ if kwargs and kwargs.get("input", None) and capture_message_content:
span.set_attribute(
- SemanticConvention.GEN_AI_CONTENT_PROMPT, str(kwargs['input'])[:1000]
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, str(kwargs["input"])[:1000]
)
# agno 2.x versions team.run use input_message instead of args[0]
- if kwargs and kwargs.get('input_message', None) and capture_message_content:
+ if kwargs and kwargs.get("input_message", None) and capture_message_content:
span.set_attribute(
- SemanticConvention.GEN_AI_CONTENT_PROMPT, str(kwargs['input_message'])[:1000]
+ SemanticConvention.GEN_AI_CONTENT_PROMPT,
+ str(kwargs["input_message"])[:1000],
)
# User and session information
@@ -171,8 +172,14 @@ def process_agent_request(
span.set_attribute(SemanticConvention.GEN_AI_SESSION_ID, kwargs["session_id"])
# agno 2.x versions use session instead of session_id
- if "session" in kwargs and kwargs["session"] and hasattr(kwargs["session"], "session_id"):
- span.set_attribute(SemanticConvention.GEN_AI_SESSION_ID, kwargs["session"].session_id)
+ if (
+ "session" in kwargs
+ and kwargs["session"]
+ and hasattr(kwargs["session"], "session_id")
+ ):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SESSION_ID, kwargs["session"].session_id
+ )
# Stream and reasoning attributes
if "stream" in kwargs:
@@ -807,44 +814,53 @@ def resolve_agno_memory_target():
>>> if module:
... print(f"Found memory class: {class_name} in {module}")
"""
- return resolve_target("agno.memory", (
- ("agno.memory.v2.memory", "Memory"),
- ("agno.memory.manager", "MemoryManager"),))
+ return resolve_target(
+ "agno.memory",
+ (
+ ("agno.memory.v2.memory", "Memory"),
+ ("agno.memory.manager", "MemoryManager"),
+ ),
+ )
def resolve_agno_knowledge_target():
"""
Resolve the appropriate Agno knowledge target class.
-
+
Attempts to find and import the correct knowledge class from Agno framework.
Tries multiple candidate modules in order of preference.
-
+
Returns:
tuple: (module_name, class_name) if successful, (None, None) if no valid target found
-
+
Example:
>>> module, class_name = resolve_agno_knowledge_target()
>>> if module:
... print(f"Found knowledge class: {class_name} in {module}")
"""
- return resolve_target("agno.knowledge", (
- ("agno.knowledge.agent", "AgentKnowledge"),
- ("agno.knowledge.knowledge", "Knowledge"),))
+ return resolve_target(
+ "agno.knowledge",
+ (
+ ("agno.knowledge.agent", "AgentKnowledge"),
+ ("agno.knowledge.knowledge", "Knowledge"),
+ ),
+ )
+
def resolve_target(target_name, candidates):
"""
Resolve a target class by trying multiple candidate modules.
-
+
This function attempts to import and find a specific class from a list of candidate
modules. It tries each candidate in order until it finds a valid module and class.
-
+
Args:
target_name (str): Name of the target being resolved (used for logging)
candidates (tuple): Tuple of (module_name, class_name) pairs to try
-
+
Returns:
tuple: (module_name, class_name) if successful, (None, None) if no valid target found
-
+
Example:
>>> candidates = (
... ("agno.memory.v2.memory", "Memory"),
diff --git a/sdk/python/src/openlit/instrumentation/mcp/async_mcp.py b/sdk/python/src/openlit/instrumentation/mcp/async_mcp.py
index 92098e44..89c9795e 100644
--- a/sdk/python/src/openlit/instrumentation/mcp/async_mcp.py
+++ b/sdk/python/src/openlit/instrumentation/mcp/async_mcp.py
@@ -70,7 +70,6 @@ def async_mcp_wrap(
):
span_kind = SpanKind.SERVER
-
# Create instrumentation context for caching expensive operations
ctx = MCPInstrumentationContext(
instance=instance,
diff --git a/sdk/python/src/openlit/instrumentation/ollama/__init__.py b/sdk/python/src/openlit/instrumentation/ollama/__init__.py
index eb4a4670..442ef4ee 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/__init__.py
@@ -8,7 +8,11 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.ollama.ollama import chat, embeddings, generate
-from openlit.instrumentation.ollama.async_ollama import async_chat, async_embeddings, async_generate
+from openlit.instrumentation.ollama.async_ollama import (
+ async_chat,
+ async_embeddings,
+ async_generate,
+)
_instruments = ("ollama >= 0.2.0",)
diff --git a/sdk/python/src/openlit/instrumentation/ollama/utils.py b/sdk/python/src/openlit/instrumentation/ollama/utils.py
index b58d9f9f..89fb9bdb 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/utils.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/utils.py
@@ -207,15 +207,14 @@ def common_chat_logic(
for tool in scope._tools:
scope._span.set_attribute(
SemanticConvention.GEN_AI_TOOL_NAME,
- tool.get("function", {}).get("name", "")
+ tool.get("function", {}).get("name", ""),
)
scope._span.set_attribute(
- SemanticConvention.GEN_AI_TOOL_CALL_ID,
- str(tool.get("id", ""))
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(tool.get("id", ""))
)
scope._span.set_attribute(
SemanticConvention.GEN_AI_TOOL_ARGS,
- str(tool.get("function", {}).get("arguments", ""))
+ str(tool.get("function", {}).get("arguments", "")),
)
# Span Attributes for Content
@@ -282,7 +281,7 @@ def common_generate_logic(
if len(scope._timestamps) > 1:
scope._tbt = calculate_tbt(scope._timestamps)
json_body = scope._kwargs.get("json", {}) or {}
- prompt = json_body.get('prompt')
+ prompt = json_body.get("prompt")
request_model = json_body.get("model") or scope._kwargs.get("model", "llama3.2")
is_stream = scope._kwargs.get("stream", False)
@@ -362,15 +361,14 @@ def common_generate_logic(
for tool in scope._tools:
scope._span.set_attribute(
SemanticConvention.GEN_AI_TOOL_NAME,
- tool.get("function", {}).get("name", "")
+ tool.get("function", {}).get("name", ""),
)
scope._span.set_attribute(
- SemanticConvention.GEN_AI_TOOL_CALL_ID,
- str(tool.get("id", ""))
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(tool.get("id", ""))
)
scope._span.set_attribute(
SemanticConvention.GEN_AI_TOOL_ARGS,
- str(tool.get("function", {}).get("arguments", ""))
+ str(tool.get("function", {}).get("arguments", "")),
)
# Span Attributes for Content
@@ -605,6 +603,7 @@ def process_streaming_generate_response(
version,
)
+
def process_generate_response(
response,
gen_ai_endpoint,
diff --git a/sdk/python/src/openlit/instrumentation/openai/utils.py b/sdk/python/src/openlit/instrumentation/openai/utils.py
index ca1cfdff..e5b2eb19 100644
--- a/sdk/python/src/openlit/instrumentation/openai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/openai/utils.py
@@ -329,7 +329,9 @@ def common_response_logic(
# Span Attributes for Response parameters
if scope._response_id:
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id
+ )
scope._span.set_attribute(
SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
)
@@ -643,7 +645,9 @@ def common_chat_logic(
# Span Attributes for Response parameters
if scope._response_id:
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id
+ )
scope._span.set_attribute(
SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
)
diff --git a/sdk/python/src/tmp/workflow.db b/sdk/python/src/tmp/workflow.db
new file mode 100644
index 00000000..e69de29b
diff --git a/sdk/python/tests/requirements.txt b/sdk/python/tests/requirements.txt
index 9da73ed2..4f236286 100644
--- a/sdk/python/tests/requirements.txt
+++ b/sdk/python/tests/requirements.txt
@@ -27,6 +27,7 @@ transformers
groq
elevenlabs
ollama
+agno
# Vector DBs
chromadb
diff --git a/sdk/python/tests/test_agno.py b/sdk/python/tests/test_agno.py
new file mode 100644
index 00000000..a0369bd5
--- /dev/null
+++ b/sdk/python/tests/test_agno.py
@@ -0,0 +1,72 @@
+# pylint: disable=duplicate-code, no-member
+"""
+This module contains tests for Agno functionality using the Agno Python library.
+
+Tests cover agent execution with Cohere models.
+These tests validate integration with OpenLIT.
+
+Environment Variables:
+ - CO_API_KEY: Cohere API key for authentication.
+
+Note: Ensure the environment is properly configured for Cohere access and OpenLIT monitoring
+prior to running these tests.
+"""
+
+from agno.agent import Agent
+from agno.models.cohere import Cohere
+import openlit
+
+# Initialize OpenLIT monitoring
+openlit.init(
+ environment="openlit-python-testing",
+ application_name="openlit-python-agno-test"
+)
+
+
+def test_agent_run():
+ """
+ Tests synchronous agent execution with Cohere.
+
+ Raises:
+ AssertionError: If the agent response is not as expected.
+ """
+ try:
+ agent = Agent(
+ model=Cohere(id="command-r-08-2024"),
+ markdown=True
+ )
+
+ response = agent.run("Say 'test passed' in 2 words")
+ assert response is not None
+ assert response.content is not None
+
+ except Exception as e:
+ print(f"Error in test_agent_run: {e}")
+ raise e
+
+
+def test_agent_async_run():
+ """
+ Tests async agent execution with Cohere.
+
+ Raises:
+ AssertionError: If the agent response is not as expected.
+ """
+ import asyncio
+
+ async def run_async():
+ try:
+ agent = Agent(
+ model=Cohere(id="command-r-08-2024"),
+ markdown=True
+ )
+
+ response = await agent.arun("Say 'async test passed' in 3 words")
+ assert response is not None
+ assert response.content is not None
+
+ except Exception as e:
+ print(f"Error in test_agent_async_run: {e}")
+ raise e
+
+ asyncio.run(run_async())