What is the right method to get json data from the...
# ask-ai
l
What is the right method to get json data from the Agent in funtion_tool @function_tool async def update_answers(self,answers:dict[str,Any]): self.session.userdata.data_collection.update_answers(answers) help me with this
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about the correct way to get JSON data from the Agent inside a
@function_tool
and how to update
userdata
with your
answers
dict. The right pattern is to accept the agent run context (commonly
RunContext
) in your function tool, ensure
userdata.data_collection
exists, then update it. Example:
Copy code
@function_tool()
async def update_answers(self, context: RunContext, answers: dict[str, Any]):
    """Update collected answers stored in session userdata."""
    # Ensure data_collection exists
    if not hasattr(context.session.userdata, "data_collection"):
        context.session.userdata.data_collection = {}

    context.session.userdata.data_collection.update(answers)
    return None, "Answers updated successfully."
And when creating the session, initialize
userdata
so
data_collection
is present:
Copy code
session = AgentSession(
    userdata={"data_collection": {}},
    stt=..., llm=..., tts=..., vad=...
)
If that doesn’t work for you, tell me: • The exact error or behavior you see (traceback/logs). • Which Agent framework version or SDK you’re using. • The structure of
self.session.userdata
in your environment (if different). I can then give a targeted fix. Sources: Agents and handoffs | LiveKit docs | Tool definition and use | LiveKit docs
l
how to give decription for answers parameter?
t
You're asking how to add a description for the
answers
parameter in your
@function_tool
. Two clear options: 1) Docstring (simple)
Copy code
@function_tool()
async def update_answers(self, context: RunContext, answers: dict[str, Any]):
    """
    Update collected answers in session userdata.

    Args:
        answers: A dict mapping question IDs to the user's answers.
                 Example: {"q1": "foo", "q2": "bar"}.
    """
    # ...
2) Typed annotation with
Annotated
+
Field
(recommended for LLM/tool schema)
Copy code
from typing import Annotated
from pydantic import Field

@function_tool()
async def update_answers(
    self,
    context: RunContext,
    answers: Annotated[dict[str, Any], Field(description="Dict mapping question IDs to answers, e.g. {'q1':'foo', 'q2':'bar'}")]
):
    # ...
Use the docstring for human readers; use
Annotated
+
Field
when you want the description included in the tool/function schema sent to the LLM. Which approach do you prefer or are you targeting LLM function-calling behavior? Sources: Building Multi-Agent Architectures with LiveKit Agents | How to test your agent using another agent | Building a Moviefone-style Theater Assistant | LiveKit docs | Tool definition and use | LiveKit docs | Azure OpenAI LLM plugin guide | LiveKit docs
l
livekit.agents._exceptions.APIStatusError: Error code: 400 - {'error': {'message': "Invalid schema for function 'update_answers': In context=('properties', 'answers'), 'additionalProperties' is required to be supplied and to be false.", 'type': 'invalid_request_error', 'param': 'tools[1].function.parameters', 'code': 'invalid_function_parameters'}} (status_code=400, request_id=147b26d6-725b-419e-b678-88f678963bd3, body={'message': "Invalid schema for function 'update_answers': In context=('properties', 'answers'), 'additionalProperties' is required to be supplied and to be false.", 'type': 'invalid_request_error', 'param': 'tools[1].function.parameters', 'code': 'invalid_function_parameters'}, retryable=True)
t
Thanks — I’m responding to the schema validation error you posted for
update_answers
(the error says the
answers
property must include
additionalProperties: false
). Two safe fixes: 1) Quick fix — supply a raw JSON schema with
additionalProperties: False
for `answers`:
Copy code
raw_schema = {
  "name": "update_answers",
  "description": "Update answers with question IDs and values",
  "parameters": {
    "type": "object",
    "properties": {
      "answers": {
        "type": "object",
        "description": "Dict mapping question IDs to answer values",
        "additionalProperties": False
      }
    },
    "required": ["answers"]
  }
}
2) Recommended — let LiveKit auto-generate a correct schema by using type hints / Pydantic instead of a manual schema:
Copy code
from livekit.agents import function_tool
from typing import Any

@function_tool()
async def update_answers(self, answers: dict[str, Any]) -> str:
    """Update collected answers (mapping question IDs to values)."""
    # implementation...
    return "Answers updated"
Which approach are you using now — raw schema or @function_tool/type hints? Share that and any current parameter definition and I’ll give the exact code to fix it. Sources: Tool definition and use | LiveKit docs