agents icon indicating copy to clipboard operation
agents copied to clipboard

Tool failed to initialise with complex nullable params

Open dvschuyl opened this issue 11 months ago • 1 comments

When decorating a Agent method with a complex parameter my program crashes at the line 162 in livekit/agents/llm/_strict.py:

    t = non_null["type"]
        ~~~~~~~~^^^^^^^^
KeyError: 'type'

A toy example:

@dataclass
class MyParam:
    x: int
    y: int | None = None
    z: list[int] = field(default_factory=list)

class MyAgent(BaseAgent):
    ...

    @function_tool
    async def my_tool(self, my_param: MyParam | None):
        ...

def entrypoint():
    ...
    session = AgentSession(
        llm=openai.LLM(model="gpt-4o-mini"),
        ...,
    )

The issue does not occur when using a realtime model.

def entrypoint():
    ...
    session = AgentSession(
        # No problem
        llm=openai.realtime.RealtimeModel(),
    )

I tried using regular Python classes, Pydantic models, TypedDicts, and dataclasses but nothing worked.

I found out the line where the crash happened relate to an optimisation. Removing these lines seem to have fixed the issue: https://github.com/livekit/agents/blob/e57f6a0a4cf07190ca4b60aff6acdadc2cebce90/livekit-agents/livekit/agents/llm/_strict.py#L152-L169

dvschuyl avatar May 15 '25 16:05 dvschuyl

It seems that in that case the variants is [{'$ref': '#/$defs/MyParam'}, {'type': 'null'}] cc @theomonnom

longcw avatar May 16 '25 09:05 longcw