agents
agents copied to clipboard
Tool failed to initialise with complex nullable params
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
It seems that in that case the variants is [{'$ref': '#/$defs/MyParam'}, {'type': 'null'}] cc @theomonnom