opik icon indicating copy to clipboard operation
opik copied to clipboard

[Bug]: List index out of range when using output schema with ADK, trace fails to upload.

Open bugthedebugger opened this issue 6 months ago • 6 comments

What component(s) are affected?

  • [x] Python SDK
  • [ ] Opik UI
  • [ ] Opik Server
  • [ ] Documentation

Opik version

  • Opik version: 1.7.16

Describe the problem

When using the output schema in Google ADK, we get this error stack after which the full trace fails to upload.

Reproduction steps and code snippets

agent = LlmAgent(
        model=selected_model,
        name=agent_name,
        instruction=system_prompt,
        tools=tools,
        before_agent_callback=opik_tracer.before_agent_callback,
        after_agent_callback=opik_tracer.after_agent_callback,
        before_model_callback=opik_tracer.before_model_callback,
        after_model_callback=opik_tracer.after_model_callback,
        before_tool_callback=opik_tracer.before_tool_callback,
        after_tool_callback=opik_tracer.after_tool_callback,
        output_schema=output_schema,
        output_key=output_key,
    )

Error logs or stack trace

Error message: Unable to serialize unknown type: <class 'pydantic._internal._model_construction.ModelMetaclass'>
Traceback (most recent call last):
  File "/Users/<username>/Documents/Projects/<project name>/<project name>-ai-agents/.venv/lib/python3.13/site-packages/opik/decorator/base_track_decorator.py", line 385, in _before_call
    start_span_arguments = self._start_span_inputs_preprocessor(
        func=func,
    ...<2 lines>...
        kwargs=kwargs,
    )
  File "/Users/<username>/Documents/Projects/<project name>/<project name>-ai-agents/.venv/lib/python3.13/site-packages/opik/integrations/adk/decorators.py", line 39, in _start_span_inputs_preprocessor
    input=convert_adk_base_models(args[0]),
          ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/Users/<username>/Documents/Projects/<project name>/<project name>-ai-agents/.venv/lib/python3.13/site-packages/opik/integrations/adk/decorators.py", line 19, in convert_adk_base_models
    return arg.model_dump(mode="json", exclude_unset=True)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<username>/Documents/Projects/<project name>/<project name>-ai-agents/.venv/lib/python3.13/site-packages/pydantic/main.py", line 463, in model_dump
    return self.__pydantic_serializer__.to_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self,
        ^^^^^
    ...<11 lines>...
        serialize_as_any=serialize_as_any,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'pydantic._internal._model_construction.ModelMetaclass'>
OPIK: Unexpected exception happened when tried to finalize span.
Output: content=Content(parts=[Part(video_metadata=None, thought=None, code_execution_result=None, executable_code=None, file_data=None, function_call=None, function_response=None, inline_data=None, text='{"session_required": true}')], role='model') grounding_metadata=None partial=None turn_complete=None error_code=None error_message=None interrupted=None custom_metadata=None
Error message: list index out of range
Traceback (most recent call last):
  File "/Users/<username>/Documents/Projects/<project name>/<project name>-ai-agents/.venv/lib/python3.13/site-packages/opik/decorator/base_track_decorator.py", line 427, in _after_call
    span_data_to_end, trace_data_to_end = pop_end_candidates()
                                          ~~~~~~~~~~~~~~~~~~^^
  File "/Users/<username>/Documents/Projects/<project name>/<project name>-ai-agents/.venv/lib/python3.13/site-packages/opik/decorator/base_track_decorator.py", line 529, in pop_end_candidates
    span_data_to_end = context_storage.pop_span_data()
  File "/Users/<username>/Documents/Projects/<project name>/<project name>-ai-agents/.venv/lib/python3.13/site-packages/opik/context_storage.py", line 50, in pop_span_data
    return stack[-1]
           ~~~~~^^^^
IndexError: list index out of range

Healthcheck results

No response

bugthedebugger avatar May 06 '25 05:05 bugthedebugger

Hi, @bugthedebugger thank your for reporting this issue. I will investigate the issue shortly and will keep you posted.

Lothiraldan avatar May 06 '25 08:05 Lothiraldan

Awesome thank you!

bugthedebugger avatar May 06 '25 09:05 bugthedebugger

@bugthedebugger pydantic._internal._model_construction.ModelMetaclass seems to be a private class in pydantic according to https://github.com/pydantic/pydantic/issues/6381. I didn't find any usage of it in https://github.com/google/adk-python. Are you importing and using it in your own code? Can you also share the version of adk and pydantic that you are using in your environment?

Lothiraldan avatar May 06 '25 09:05 Lothiraldan

google-adk = "^0.3.0" pydantic = ">=2.0.0,<3.0.0" <- this is part of adk dependency not added manually by as atm.

Here's a snippet of outputschema that we passed.

from pydantic import BaseModel, Field

class SessionSelectorResponse(BaseModel):
    session_required: bool = Field(description="Whether previous conversations are needed to answer the user query.")


agent = LlmAgent(
        model=selected_model,
        name=agent_name,
        instruction=system_prompt,
        before_agent_callback=opik_tracer.before_agent_callback,
        after_agent_callback=opik_tracer.after_agent_callback,
        before_model_callback=opik_tracer.before_model_callback,
        after_model_callback=opik_tracer.after_model_callback,
        before_tool_callback=opik_tracer.before_tool_callback,
        after_tool_callback=opik_tracer.after_tool_callback,
        output_schema=SessionSelectorResponse,
        output_key='data',
    )

bugthedebugger avatar May 06 '25 09:05 bugthedebugger

@bugthedebugger We have a few issues with the ADK library, @alexkuzmik is working on a number of fixes for the ADK integration that will include support for multip-agent support

jverre avatar May 09 '25 14:05 jverre

First PR with improvements is open here, after this significant revamp of the integration we'll release some additional improvements. More coming soon !

jverre avatar May 13 '25 16:05 jverre

Hi @bugthedebugger, the PR has been released, did you had a chance to try it out?

Lothiraldan avatar May 26 '25 07:05 Lothiraldan

@Lothiraldan updated to latest version and looks good 👍 You can close out the issue if all good from your end too. Thank you!

bugthedebugger avatar May 27 '25 04:05 bugthedebugger