Opik tracing gives Object of type ChatCompletionMessageToolCall is not JSON serializable
Describe the bug Trying to use Opik with Ollama and LiteLlm raise the error TypeError: Object of type ChatCompletionMessageToolCall is not JSON serializable.
The initial trace it's send but the response of the model is the issue.
To Reproduce Steps to reproduce the behavior: Here's the code:
import datetime
from zoneinfo import ZoneInfo
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
import os
from google.adk import Agent, Runner
from google.adk.sessions import InMemorySessionService
from google.adk.tools.crewai_tool import CrewaiTool
from google.genai import types
from opik import track
from litellm.integrations.opik.opik import OpikLogger
import litellm
opik_logger = OpikLogger()
litellm.callbacks = [opik_logger]
def get_weather(city: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city for which to retrieve the weather report.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
return {
"status": "success",
"report": (
"The weather in New York is sunny with a temperature of 25 degrees"
" Celsius (41 degrees Fahrenheit)."
),
}
else:
return {
"status": "error",
"error_message": f"Weather information for '{city}' is not available.",
}
def get_current_time(city: str) -> dict:
"""Returns the current time in a specified city.
Args:
city (str): The name of the city for which to retrieve the current time.
Returns:
dict: status and result or error msg.
"""
if city.lower() == "new york":
tz_identifier = "America/New_York"
else:
return {
"status": "error",
"error_message": (
f"Sorry, I don't have timezone information for {city}."
),
}
tz = ZoneInfo(tz_identifier)
now = datetime.datetime.now(tz)
report = (
f'The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
)
return {"status": "success", "report": report}
def get_current_population(city:str) -> dict:
if city.lower() == "new york":
return {
"status": "success",
"report": (
"The population is 500000 people"
),
}
else:
return {
"status": "error",
"error_message": f"Population information for '{city}' is not available.",
}
root_agent = Agent(
model=LiteLlm(
model='ollama_chat/llama3.1'
),
name="weather_time_agent",
description=(
"Agent to answer questions about the time and weather in a city."
),
instruction=(
"You are a helpful agent who can answer user questions about the time and weather in a city."
),
tools=[get_weather, get_current_time, get_current_population],
)
APP_NAME = "news_app"
USER_ID = "user1234"
SESSION_ID = "1234"
# Session and Runner
session_service = InMemorySessionService()
session = session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID)
runner = Runner(agent=root_agent, app_name=APP_NAME, session_service=session_service)
# Agent Interaction
@track
def call_agent(query):
content = types.Content(role='user', parts=[types.Part(text=query)])
events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content)
for event in events:
if event.is_final_response():
final_response = event.content.parts[0].text
print("Agent Response: ", final_response)
call_agent("what is the weather in new york? ")
call_agent("what about population ?")
and here full log:
23:53:01 - LiteLLM:INFO: utils.py:3085 -
LiteLLM completion() model= llama3.1; provider = ollama_chat
2025-04-13 23:53:01,737 - INFO - utils.py:3085 -
LiteLLM completion() model= llama3.1; provider = ollama_chat
2025-04-13 23:53:01,764 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
23:53:01 - LiteLLM:INFO: cost_calculator.py:636 - selected model name for cost calculation: ollama_chat/llama3.1
2025-04-13 23:53:01,767 - INFO - cost_calculator.py:636 - selected model name for cost calculation: ollama_chat/llama3.1
2025-04-13 23:53:01,767 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
2025-04-13 23:53:01,799 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
2025-04-13 23:53:01,824 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
23:53:03 - LiteLLM:INFO: cost_calculator.py:636 - selected model name for cost calculation: ollama_chat/llama3.1
2025-04-13 23:53:03,359 - INFO - cost_calculator.py:636 - selected model name for cost calculation: ollama_chat/llama3.1
2025-04-13 23:53:03,380 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
2025-04-13 23:53:03,382 - INFO - fast_api.py:616 - Generated event in agent run streaming: {"content":{"parts":[{"text":"The current weather in New York is sunny with a temperature of 25 degrees Celsius (41 degrees Fahrenheit)."}],"role":"model"},"partial":false,"invocation_id":"e-ea4953c0-7667-4839-86de-1e5a2aad69cb","author":"weather_time_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"id":"VZnsT0aW","timestamp":1744581181.733358}
2025-04-13 23:53:03,412 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
23:53:03 - LiteLLM:INFO: cost_calculator.py:636 - selected model name for cost calculation: ollama_chat/llama3.1
2025-04-13 23:53:03,414 - INFO - cost_calculator.py:636 - selected model name for cost calculation: ollama_chat/llama3.1
2025-04-13 23:53:03,432 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
2025-04-13 23:53:03,451 - INFO - _client.py:1025 - HTTP Request: POST http://localhost:11434/api/show "HTTP/1.1 200 OK"
INFO: 127.0.0.1:51176 - "GET /apps/multi_tool_agent/users/user/sessions/3c9b4998-9f88-4728-8d9e-6e7639136e64 HTTP/1.1" 200 OK
23:53:04 - LiteLLM:INFO: opik.py:166 - Calling async_send_batch
2025-04-13 23:53:04,831 - INFO - opik.py:166 - Calling async_send_batch
23:53:04 - LiteLLM:ERROR: opik.py:154 - OpikLogger failed to send batch - Object of type ChatCompletionMessageToolCall is not JSON serializable
Traceback (most recent call last):
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/integrations/opik/opik.py", line 140, in _submit_batch
response = await self.async_httpx_client.post(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/litellm_core_utils/logging_utils.py", line 135, in async_wrapper
result = await func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 258, in post
raise e
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 208, in post
req = self.client.build_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_client.py", line 378, in build_request
return Request(
^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_models.py", line 408, in __init__
headers, stream = encode_request(
^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 216, in encode_request
return encode_json(json)
^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 177, in encode_json
body = json_dumps(
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 180, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ChatCompletionMessageToolCall is not JSON serializable
2025-04-13 23:53:04,832 - ERROR - opik.py:154 - OpikLogger failed to send batch - Object of type ChatCompletionMessageToolCall is not JSON serializable
Traceback (most recent call last):
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/integrations/opik/opik.py", line 140, in _submit_batch
response = await self.async_httpx_client.post(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/litellm_core_utils/logging_utils.py", line 135, in async_wrapper
result = await func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 258, in post
raise e
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 208, in post
req = self.client.build_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_client.py", line 378, in build_request
return Request(
^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_models.py", line 408, in __init__
headers, stream = encode_request(
^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 216, in encode_request
return encode_json(json)
^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 177, in encode_json
body = json_dumps(
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 180, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ChatCompletionMessageToolCall is not JSON serializable
23:53:04 - LiteLLM:INFO: opik.py:178 - Sent 2 traces
2025-04-13 23:53:04,834 - INFO - opik.py:178 - Sent 2 traces
23:53:04 - LiteLLM:ERROR: opik.py:154 - OpikLogger failed to send batch - Object of type ChatCompletionMessageToolCall is not JSON serializable
Traceback (most recent call last):
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/integrations/opik/opik.py", line 140, in _submit_batch
response = await self.async_httpx_client.post(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/litellm_core_utils/logging_utils.py", line 135, in async_wrapper
result = await func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 258, in post
raise e
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 208, in post
req = self.client.build_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_client.py", line 378, in build_request
return Request(
^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_models.py", line 408, in __init__
headers, stream = encode_request(
^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 216, in encode_request
return encode_json(json)
^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 177, in encode_json
body = json_dumps(
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 180, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ChatCompletionMessageToolCall is not JSON serializable
2025-04-13 23:53:04,834 - ERROR - opik.py:154 - OpikLogger failed to send batch - Object of type ChatCompletionMessageToolCall is not JSON serializable
Traceback (most recent call last):
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/integrations/opik/opik.py", line 140, in _submit_batch
response = await self.async_httpx_client.post(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/litellm_core_utils/logging_utils.py", line 135, in async_wrapper
result = await func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 258, in post
raise e
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/litellm/llms/custom_httpx/http_handler.py", line 208, in post
req = self.client.build_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_client.py", line 378, in build_request
return Request(
^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_models.py", line 408, in __init__
headers, stream = encode_request(
^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 216, in encode_request
return encode_json(json)
^^^^^^^^^^^^^^^^^
File "/Users/joao/projects/adk-python/venv/lib/python3.11/site-packages/httpx/_content.py", line 177, in encode_json
body = json_dumps(
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 180, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ChatCompletionMessageToolCall is not JSON serializable
23:53:04 - LiteLLM:INFO: opik.py:183 - Sent 2 spans
2025-04-13 23:53:04,835 - INFO - opik.py:183 - Sent 2 spans
Expected behavior The output should also be sent, maybe is something especific with ollama, I wil test later on with gemini.
Screenshots
Desktop (please complete the following information):
- OS: macOs 15.3.2 (24D81)
- Python version(python -V): 3.11
- ADK version(pip show google-adk): 0.1.0
@INFINIVagas please try Gemini and see if it works.
Ollama has more various formats so it might not be that widely compatible.
Closing this and feel free to re-open if you still see issues.