square-flag-66304
11/24/2025, 6:25 AMtall-airline-16756
11/24/2025, 6:25 AMtall-airline-16756
11/24/2025, 6:26 AMRunContext[UserData]. This commonly happens after a dependency/version change (pydantic or agent/plugin) that tightened schema generation.
• The conversion path failing is: build_legacy_openai_schema(...) → function_arguments_to_pydantic_model → create_model → generate_schema — failing on RunContext[UserData].
Practical fixes (fastest first)
1) Immediate fix — allow arbitrary types
• Add Pydantic config to your UserData so Pydantic will accept it without generating a strict schema:
from pydantic import ConfigDict
@dataclass
class UserData:
user_name: str | None = None
age: int | None = None
model_config = ConfigDict(arbitrary_types_allowed=True)
This usually unblocks the function-schema conversion quickly.
2) Safer refactor — avoid generic typed RunContext[...] in tool signatures
• Remove the generic typing from the function parameter so schema generation won’t try to process it:
@function_tool()
async def my_tool(self, context, location: str):
userdata: UserData = context.userdata
or accept only serializable inputs and access context.userdata inside the body.
3) Use explicit Pydantic input models for tool arguments
• Replace passing typed RunContext fields with a Pydantic model that contains only the needed serializable fields:
from pydantic import BaseModel
class NameInput(BaseModel):
name: str
@function_tool()
async def record_name(self, context: RunContext, input: NameInput):
context.userdata.user_name = input.name
4) Advanced — implement custom pydantic core schema (if you must keep complex types)
• Implement __get_pydantic_core_schema__ on UserData (advanced, more work). Only needed if you require full schema support for that type.
Checks to run now
• Verify UserData doesn’t embed non-serializable objects (Room, locks, participants, etc.).
• Check recent package updates for pydantic, livekit-agents, or the Gemini plugin that might have changed schema behavior.
If you want, I can:
• Suggest the minimal code change (showing exact file/line) to add model_config if you paste your UserData definition, or
• Help you refactor one tool signature you paste here so it avoids the generic type and works with Gemini function conversion.
Which would you prefer?
Sources: AI Agents Framework | livekit/livekit_composite | DeepWiki | Gemini Live API plugin | LiveKit docs | Tool definition and use | LiveKit docs | Agents and handoffs | LiveKit docssquare-flag-66304
11/24/2025, 6:31 AMarbitrary_types_allowed=True in the model_config to ignore this error or implement __get_pydantic_core_schema__ on your type to fully support it. If you got this error by calling handler(<some type>) within __get_pydantic_core_schema__ then you likely need to call handler.generate_schema(<some type>) since we do not call __get_pydantic_core_schema__ on <some type> otherwise to avoid infinite recursion. For further information visit https://errors.pydantic.dev/2.12/u/schema-for-unknown-type The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Intel_IL\AINA_voice_bot\AGENT\venv\lib\site-packages\livekit\agents\llm\llm.py", line 190, in _main_task return await self._run() File "C:\Intel_IL\AINA_voice_bot\AGENT\venv\lib\site-packages\livekit\plugins\google\llm.py", line 423, in _run raise APIConnectionError( livekit.agents._exceptions.APIConnectionError: gemini llm: error generating content Unable to generate pydantic-core schema for livekit.agents.voice.events.RunContext[__mp_main__.UserData]. Set arbitrary_types_allowed=True in the model_config to ignore this error or implement __get_pydantic_core_schema__ on your type to fully support it. If you got this error by calling handler(<some type>) within __get_pydantic_core_schema__ then you likely need to call handler.generate_schema(<some type>) since we do not call __get_pydantic_core_schema__ on <some type> otherwise to avoid infinite recursion.tall-airline-16756
11/24/2025, 6:32 AMRunContext[UserData] when converting your tool to a Gemini function schema.
Next step I can take to help: please paste the UserData class definition and the tool function signature(s) (the @function_tool function(s) that reference RunContext[...]) so I can suggest the exact minimal change.
If you prefer a quick workaround right away, add this to your UserData definition:
from pydantic import ConfigDict
@dataclass
class UserData:
user_name: str | None = None
age: int | None = None
model_config = ConfigDict(arbitrary_types_allowed=True)
That typically unblocks schema generation. If you paste your definitions I’ll confirm that change or give a safer refactor (remove generic typing from the function signature or replace the input with a serializable Pydantic model) tailored to your code.square-flag-66304
11/24/2025, 6:33 AMtall-airline-16756
11/24/2025, 6:33 AMRunContext[UserData] schema error in your transfer_to_quiz tool.
Recommended immediate fix (minimal change)
• Add Pydantic config to UserData so Pydantic will skip strict schema generation:
from pydantic import ConfigDict
from dataclasses import dataclass, field
from typing import Optional, Callable, Dict, List
from datetime import datetime
@dataclass
class UserData:
user_id: str = ""
personas: Dict[str, "Agent"] = field(default_factory=dict)
persona_factories: Dict[str, Callable[[], "Agent"]] = field(default_factory=dict)
prev_agent: Optional["Agent"] = None
ctx: Optional["JobContext"] = None
quiz_answers: List = field(default_factory=list)
insights: List = field(default_factory=list)
recommendations: List = field(default_factory=list)
quiz_completed: bool = False
counselling_Booked: bool = False
course_enrolled: bool = False
created_at: datetime = field(default_factory=datetime.now)
last_modified: datetime = field(default_factory=datetime.now)
student_name: Optional[str] = None
student_grade: Optional[str] = None
students_choice_of_exam: Optional[str] = None
last_agent_name: Optional[str] = None
conversation_id: Optional[str] = None
subject: Optional[str] = None
model_config = ConfigDict(arbitrary_types_allowed=True)
This typically unblocks the schema generation and should stop the Gemini/pydantic error.
Safer refactor (if you prefer avoiding arbitrary types)
• Remove the generic RunContext[UserData] from the tool signature so Pydantic doesn’t attempt to generate a schema for it:
@function_tool()
async def transfer_to_quiz(self, context) -> Agent:
userdata = context.userdata
...
or accept only serializable inputs and access context.userdata inside the function body, or pass a Pydantic BaseModel containing only the fields you need.
Which option would you like me to expand into a patch for your repository? If you want, paste the file path(s) and I’ll show the exact diff to apply.square-flag-66304
11/24/2025, 6:35 AMtall-airline-16756
11/24/2025, 6:36 AMcontext.userdata) into your tool regardless of whether you annotate the parameter as RunContext[UserData]. So changing the signature to async def transfer_to_quiz(self, context) will still let you access userdata = context.userdata inside the function.
• The reason we remove RunContext[UserData] is to avoid Pydantic trying to generate a function schema for that generic type. The LLM/function-calling layer only needs serializable inputs — it doesn't need the full RunContext schema — so keeping context untyped prevents the schema error while preserving runtime behavior.
Best practice options:
• Keep the parameter untyped (context) and use context.userdata inside the body.
• Or accept a small serializable Pydantic BaseModel with only the fields the tool needs (preferred if you want the LLM to supply structured arguments).
If you want, I can show the exact signature change or convert the fields your tool needs into a simple BaseModel and show that patch. Which would you prefer?square-flag-66304
11/24/2025, 6:37 AMtall-airline-16756
11/24/2025, 6:38 AMcontext parameter — keep it so you can access runtime state (like context.userdata). Instead do one of these minimal changes:
• Easiest: keep context but remove the RunContext[UserData] typing so Pydantic won’t try to generate a schema:
@function_tool()
async def transfer_to_quiz(self, context) -> Agent:
userdata = context.userdata
...
• If you want LLM-driven structured inputs: remove context from the function signature and accept only a serializable Pydantic model with the fields the LLM should supply, then read/update context.userdata from elsewhere (e.g., the agent/session). Example:
class QuizInput(BaseModel):
student_name: str
students_choice_of_exam: Optional[str] = None
@function_tool()
async def transfer_to_quiz(self, input: QuizInput) -> Agent:
# access session context via self.session or self.chat_ctx
...
• Or keep the typed signature but add this to UserData as a quick unblock:
from pydantic import ConfigDict
@dataclass
class UserData:
...
model_config = ConfigDict(arbitrary_types_allowed=True)
Recommendation: change the signature to accept untyped context (first option). It’s the smallest change and preserves runtime behavior while avoiding the Pydantic/Gemini error. Would you like the exact patch/diff for your file?square-flag-66304
11/24/2025, 6:39 AMtall-airline-16756
11/24/2025, 6:39 AMRunContext[UserData], and 2) when you remove the type hint entirely the schema builder trips on a missing type for "context" (KeyError). Don't remove `context`; instead exclude it from the LLM-visible schema so runtime injection still works but schema generation skips it.
Minimal fix — use Annotated + ExcludeFromSchema
1. Import Annotated and the ExcludeFromSchema marker:
from typing import Annotated
from livekit.agents.llm import ExcludeFromSchema
from livekit.agents.events import RunContext
1. Update your tool signature:
@function_tool()
async def transfer_to_quiz(
self,
context: Annotated[RunContext[UserData], ExcludeFromSchema]
) -> Agent:
userdata = context.userdata
...
This tells LiveKit to skip context when building the Pydantic model for Gemini, preventing both the schema generation error and the KeyError.
Optional safety: keep UserData tolerant If you still hit schema issues for other reasons, add this to UserData as a fast unblock:
from pydantic import ConfigDict
@dataclass
class UserData:
...
model_config = ConfigDict(arbitrary_types_allowed=True)
But prefer the Annotated+ExcludeFromSchema approach first.
Would you like me to generate the exact diff for the file you showed?
Sources: AI Agents Framework | livekit/livekit_composite | DeepWiki | Agents v0.x migration guide - Python | LiveKit docs | Tool definition and use | LiveKit docssquare-flag-66304
11/24/2025, 6:48 AMtall-airline-16756
11/24/2025, 6:49 AMExcludeFromSchema isn't available in your install. Two safe options:
1) Quick workaround (no special imports)
• Keep context parameter but type it as plain RunContext (no generic) so schema builder can skip complex generics:
from livekit.agents.events import RunContext
@function_tool()
async def transfer_to_quiz(self, context: RunContext) -> Agent:
userdata = context.userdata
...
• Also add the UserData Pydantic tolerance to be safe:
from pydantic import ConfigDict
@dataclass
class UserData:
...
model_config = ConfigDict(arbitrary_types_allowed=True)
2) Preferred fix if your LiveKit version supports it
• Use an explicit exclusion marker. If your package exposes it, import and use Annotated to exclude context from the LLM schema:
from typing import Annotated
from livekit.agents.llm import ExcludeFromSchema
from livekit.agents.events import RunContext
@function_tool()
async def transfer_to_quiz(self, context: Annotated[RunContext[UserData], ExcludeFromSchema]) -> Agent:
userdata = context.userdata
...
If from livekit.agents.llm import ExcludeFromSchema raises ImportError, that marker isn't present in your installed release.
Which would you like to try first? If you want, tell me your livekit-agents and pydantic versions (pip show livekit-agents pydantic) and I’ll recommend the exact change.
Sources: Company directory phone assistant | LiveKit Docs | Tool definition and use | LiveKit docs | Models | LiveKit Docssquare-flag-66304
11/24/2025, 6:51 AMtall-airline-16756
11/24/2025, 6:52 AM