Skip to content

Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI

update serializer

reibs41cb27733b70
diff --git a/agentops/langchain_callback_handler.py b/agentops/langchain_callback_handler.py
index a807be07..cdb51863 100644
--- a/agentops/langchain_callback_handler.py
+++ b/agentops/langchain_callback_handler.py
@@ -87,9 +87,13 @@ class LangchainCallbackHandler(BaseCallbackHandler):
**kwargs: Any,
) -> Any:
self.events[run_id].end_timestamp = get_ISO_time()
- self.events[run_id].returns = response.generations[0][0].message.content
- self.events[run_id].prompt_tokens = response.llm_output['token_usage']['prompt_tokens']
- self.events[run_id].completion_tokens = response.llm_output['token_usage']['completion_tokens']
+ self.events[run_id].returns = {
+ "content": response.generations[0][0].message.content,
+ "generations": response.generations
+ }
+ if response.llm_output is not None:
+ self.events[run_id].prompt_tokens = response.llm_output['token_usage']['prompt_tokens']
+ self.events[run_id].completion_tokens = response.llm_output['token_usage']['completion_tokens']
if len(response.generations) > 0:
self.events[run_id].result = "Success"
@@ -357,9 +361,13 @@ class AsyncLangchainCallbackHandler(AsyncCallbackHandler):
**kwargs: Any,
) -> Any:
self.events[run_id].end_timestamp = get_ISO_time()
- self.events[run_id].returns = response.generations[0][0].message.content
- self.events[run_id].prompt_tokens = response.llm_output['token_usage']['prompt_tokens']
- self.events[run_id].completion_tokens = response.llm_output['token_usage']['completion_tokens']
+ self.events[run_id].returns = {
+ "content": response.generations[0][0].message.content,
+ "generations": response.generations
+ }
+ if response.llm_output is not None:
+ self.events[run_id].prompt_tokens = response.llm_output['token_usage']['prompt_tokens']
+ self.events[run_id].completion_tokens = response.llm_output['token_usage']['completion_tokens']
if len(response.generations) > 0:
self.events[run_id].result = "Success"
diff --git a/agentops/worker.py b/agentops/worker.py
index 618bf5b5..430807af 100644
--- a/agentops/worker.py
+++ b/agentops/worker.py
@@ -28,7 +28,13 @@ def filter_unjsonable(d: dict) -> dict:
def safe_serialize(obj):
- def default(o): return f"<<non-serializable: {type(o).__qualname__}>>"
+ def default(o):
+ if hasattr(o, 'model_dump_json'):
+ return o.model_dump_json()
+ elif hasattr(o, 'to_json'):
+ return o.to_json()
+ else:
+ return f"<<non-serializable: {type(o).__qualname__}>>"
return json.dumps(obj, default=default)