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]: coroutine `async_model_run_function_call_wrap.<locals>.wrapper` was never awaited (#849)

Patcher4dccc1b8e539
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 72485f03..e58c9dec 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.34.42"
+version = "1.34.43"
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/instrumentation/agno/__init__.py b/sdk/python/src/openlit/instrumentation/agno/__init__.py
index 4f5f3ac4..264008a4 100644
--- a/sdk/python/src/openlit/instrumentation/agno/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/agno/__init__.py
@@ -29,6 +29,7 @@ from openlit.instrumentation.agno.agno import (
)
from openlit.instrumentation.agno.async_agno import (
async_agent_run_wrap,
+ async_agent_run_stream_wrap,
async_agent_continue_run_wrap,
async_function_execute_wrap,
async_reasoning_wrap,
@@ -93,9 +94,9 @@ class AgnoInstrumentor(BaseInstrumentor):
# Workflow Operations (Always Instrumented)
wrap_function_wrapper(
"agno.agent.agent",
- "Agent.run",
+ "Agent._run",
agent_run_wrap(
- "agent.run",
+ "agent._run",
version,
environment,
application_name,
@@ -143,9 +144,25 @@ class AgnoInstrumentor(BaseInstrumentor):
# Async Operations
wrap_function_wrapper(
"agno.agent.agent",
- "Agent.arun",
+ "Agent._arun",
async_agent_run_wrap(
- "agent.arun",
+ "agent._arun",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
+ )
+
+ wrap_function_wrapper(
+ "agno.agent.agent",
+ "Agent._arun_stream",
+ async_agent_run_stream_wrap(
+ "agent._arun_stream",
version,
environment,
application_name,
@@ -209,38 +226,10 @@ class AgnoInstrumentor(BaseInstrumentor):
),
)
- # CRITICAL: Also instrument the batch function calls method
- wrap_function_wrapper(
- "agno.models.base",
- "Model.run_function_calls",
- model_run_function_call_wrap(
- "model.run_function_calls",
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- ),
- )
-
- wrap_function_wrapper(
- "agno.models.base",
- "Model.arun_function_calls",
- async_model_run_function_call_wrap(
- "model.arun_function_calls",
- version,
- environment,
- application_name,
- tracer,
- pricing_info,
- capture_message_content,
- metrics,
- disable_metrics,
- ),
- )
+ # NOTE: Removed instrumentation for Model.run_function_calls and Model.arun_function_calls
+ # These methods are iterators/async iterators that call individual function calls
+ # Individual function calls are already instrumented through Model.run_function_call and Model.arun_function_call
+ # Instrumenting the batch methods causes span hierarchy issues and is redundant
# CRITICAL: Memory operations that bypass model bridge
wrap_function_wrapper(
diff --git a/sdk/python/src/openlit/instrumentation/agno/async_agno.py b/sdk/python/src/openlit/instrumentation/agno/async_agno.py
index 3a58c739..94a7d8f2 100644
--- a/sdk/python/src/openlit/instrumentation/agno/async_agno.py
+++ b/sdk/python/src/openlit/instrumentation/agno/async_agno.py
@@ -53,21 +53,9 @@ def async_agent_run_wrap(
)
span_name = f"agent {agent_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.CLIENT, context=current_context
- ) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) 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:
- response = await wrapped(*args, **kwargs)
- finally:
- context_api.detach(token)
+ response = await wrapped(*args, **kwargs)
try:
# Process request using utils function with ALL attributes from semcov
@@ -180,31 +168,28 @@ def async_model_run_function_call_wrap(
return await wrapped(*args, **kwargs)
# Extract function call information for span naming
- function_call = args[0] if args else None
- function_name = "unknown_function"
- if function_call and hasattr(function_call, "function"):
- if hasattr(function_call.function, "name"):
+ function_call = args[0] if args else kwargs.get("function_call", None)
+ function_name = None
+
+ if function_call:
+ # Extract function name from the FunctionCall object
+ if hasattr(function_call, "function") and hasattr(
+ function_call.function, "name"
+ ):
function_name = function_call.function.name
- elif hasattr(function_call.function, "__name__"):
- function_name = function_call.function.__name__
+ elif hasattr(function_call, "name"):
+ function_name = function_call.name
- span_name = f"model run function {function_name}"
+ # Skip creating span if we can't identify the function
+ if not function_name or function_name == "unknown_function":
+ return await wrapped(*args, **kwargs)
- # CRITICAL: Capture current OpenTelemetry context to ensure proper nesting
- current_context = context_api.get_current()
+ span_name = f"tool {function_name}"
- 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
process_tool_request(
@@ -237,6 +222,181 @@ def async_model_run_function_call_wrap(
return wrapper
+def async_agent_run_stream_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
+ """
+ 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)
+ or getattr(instance, "agent_id", None)
+ or "default_agent"
+ )
+ span_name = f"agent {agent_name}"
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
+
+ try:
+ async for response in wrapped(*args, **kwargs):
+ yield response
+
+ # Get the final response after iteration completes
+ final_response = getattr(instance, "run_response", None)
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error("Error in async agent._arun_stream: %s", e)
+ raise
+
+ 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))
+
+ 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
+
+
+def async_model_run_function_calls_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
+ """
+ Wrap Agno Model.arun_function_calls async method to bridge agent and tool span context.
+ This method returns an AsyncIterator and requires special handling to maintain span hierarchy.
+ """
+
+ 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 = []
+ if function_calls:
+ for fc in function_calls:
+ if hasattr(fc, "function"):
+ if hasattr(fc.function, "name"):
+ function_names.append(fc.function.name)
+ elif hasattr(fc.function, "__name__"):
+ function_names.append(fc.function.__name__)
+
+ span_name = f"model run functions {', '.join(function_names[:3])}" # Limit to first 3 names
+ 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:
+ 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:
+ yield item
+
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error(
+ "Error in async model.arun_function_calls execution: %s", e
+ )
+ raise
+ finally:
+ # Process the span after completion
+ try:
+ # Process request using utils function with ALL attributes from semcov
+ process_tool_request(
+ span,
+ function_calls,
+ args,
+ kwargs,
+ "AsyncIterator completed", # Result is the completion status
+ start_time,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ "batch_function_calls",
+ )
+
+ span.set_status(Status(StatusCode.OK))
+
+ except Exception as e:
+ handle_exception(span, e)
+ logger.error(
+ "Error in async model.arun_function_calls trace creation: %s", e
+ )
+
+ return wrapper
+
+
def async_function_entrypoint_wrap(
gen_ai_endpoint,
version,