fix: enhance LLM event handling with task and agent metadata (#3422)
Some checks failed
Notify Downstream / notify-downstream (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled

* fix: enhance LLM event handling with task and agent metadata

- Added `from_task` and `from_agent` parameters to LLM event emissions for improved traceability.
- Updated `_send_events_to_backend` method in TraceBatchManager to return status codes for better error handling.
- Modified `CREWAI_BASE_URL` to remove trailing slash for consistency.
- Improved logging and graceful failure handling in event sending process.

* drop print
This commit is contained in:
Lorenze Jay
2025-08-29 13:48:49 -07:00
committed by GitHub
parent e4c4b81e63
commit 2633b33afc
3 changed files with 12 additions and 5 deletions

View File

@@ -950,6 +950,8 @@ class LLM(BaseLLM):
tool_name=function_name,
tool_args=function_args,
error=f"Tool execution error: {str(e)}",
from_task=from_task,
from_agent=from_agent,
),
)
return None

View File

@@ -16,4 +16,4 @@ class _NotSpecified:
# Unlike `None`, which might be a valid value from the user, `NOT_SPECIFIED` allows
# us to distinguish between "not passed at all" and "explicitly passed None" or "[]".
NOT_SPECIFIED = _NotSpecified()
CREWAI_BASE_URL = "https://app.crewai.com/"
CREWAI_BASE_URL = "https://app.crewai.com"

View File

@@ -150,10 +150,10 @@ class TraceBatchManager:
"""Add event to buffer"""
self.event_buffer.append(trace_event)
def _send_events_to_backend(self):
def _send_events_to_backend(self) -> int:
"""Send buffered events to backend with graceful failure handling"""
if not self.plus_api or not self.trace_batch_id or not self.event_buffer:
return
return 500
try:
payload = {
@@ -173,19 +173,22 @@ class TraceBatchManager:
if response is None:
logger.warning("Failed to send trace events. Events will be lost.")
return
return 500
if response.status_code in [200, 201]:
self.event_buffer.clear()
return 200
else:
logger.warning(
f"Failed to send events: {response.status_code}. Events will be lost."
)
return 500
except Exception as e:
logger.warning(
f"Error sending events to backend: {str(e)}. Events will be lost."
)
return 500
def finalize_batch(self) -> Optional[TraceBatch]:
"""Finalize batch and return it for sending"""
@@ -194,7 +197,9 @@ class TraceBatchManager:
self.current_batch.events = self.event_buffer.copy()
if self.event_buffer:
self._send_events_to_backend()
events_sent_to_backend_status = self._send_events_to_backend()
if events_sent_to_backend_status == 500:
return None
self._finalize_backend_batch()
finalized_batch = self.current_batch