early-window-29567
11/17/2025, 3:10 PM@function_tool
@observe()
async def send_template_data(
context: RunContext[SessionState],
template_data: Annotated[
dict[str, Any],
TypeInfo(
description="Dictionary containing structured data matching the lesson's template_schema. For example: {'got_it_right': true, 'accuracy_percentage': 85, 'comments_for_improvement': '...'}",
),
],
):tall-airline-16756
11/17/2025, 3:10 PMtall-airline-16756
11/17/2025, 3:11 PMdict[str, Any], and incorporate Annotated types to add metadata about the arguments. This helps the system understand structured input requirements, including nested objects and specific data constraints.
• Using Raw JSON Schema: If you'd like more control, you can define tools using a raw JSON schema through the raw_schema parameter in the @function_tool decorator. This approach lets you explicitly specify complex input structures, such as dictionaries with detailed property definitions, nested objects, and required fields.
Both of these methods are great for working effectively with structured data. If you'd like, I’m here to provide more details or walk you through some examples to help you get started!
Sources: Tool definition and use | LiveKit docs | livekit-agents/livekit/agents/llm/utils.py | mcp_client/agent_tools.pyearly-window-29567
11/17/2025, 3:12 PMtall-airline-16756
11/17/2025, 3:13 PM@function_tool
@observe()
async def send_template_data(
context: RunContext[SessionState],
template_data: Annotated[
dict[str, Any],
TypeInfo(
description="Dictionary containing structured data matching the lesson's template_schema. For example: {'got_it_right': true, 'accuracy_percentage': 85, 'comments_for_improvement': '...'}",
),
],
):
pass
• Using Raw JSON Schema: If you want a bit more control, you can define tools using a raw JSON schema. Here's how you might do it:
raw_schema = {
"type": "function",
"name": "get_weather",
"description": "Get weather for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City and country e.g. New York"}
},
"required": ["location"],
"additionalProperties": False
}
}
@function_tool(raw_schema=raw_schema)
async def get_weather(raw_arguments: dict[str, object], context: RunContext):
location = raw_arguments["location"]
return f"The weather of {location} is ..."
If you're interested in exploring more, here are some great examples you can check out:
• Annotated Tool Arguments Example
• Raw JSON Schema Example
• Dynamic Tool Creation Example
Please feel free to reach out if you'd like me to clarify anything or provide more examples—I'm here to help!
Sources: livekit-plugins/livekit-plugins-anthropic/livekit/plugins/anthropic/utils.p... | Tool definition and use | LiveKit docs | mcp_client/agent_tools.pyearly-window-29567
11/17/2025, 3:14 PMtall-airline-16756
11/17/2025, 3:15 PMearly-window-29567
11/17/2025, 3:36 PMtall-airline-16756
11/17/2025, 3:37 PMRunContext and UserData for managing persistent state. Let me walk you through how you can do it:
• Define a UserData Class: Start by creating a dataclass to keep track of your session-level state. Here's an example:
from dataclasses import dataclass, field
from typing import List, Dict, Any
@dataclass
class UserData:
user_name: str = ""
task_list: List[str] = field(default_factory=list)
• Access and Modify State in Function Tools: You can update the state easily by using context.userdata. For instance:
@function_tool
async def add_task(self, task: str, context: RunContext[UserData]):
context.userdata.task_list.append(task)
return f"Task '{task}' added. Total tasks: {len(context.userdata.task_list)}"
• Update Room Attributes: If you want, you can also update room-level attributes like this:
await context.userdata.ctx.room.local_participant.set_attributes({
"task_count": str(len(context.userdata.task_list))
})
This method helps keep your state persistent and available across different function calls. If anything is unclear or you'd like me to provide more examples, just let me know—I'm here to help!
Sources: context-readmes/USERDATA_GUIDE.md | livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | Tool definition and use | LiveKit docs