whats the correct way to pass function context to ...
# ask-ai
p
whats the correct way to pass function context to agent? for RAG etc. i keep getting errors. {"level": "ERROR", "name": "livekit.agents", "message": "unhandled exception while running the job task\nTraceback (most recent call last):\n File \"/home/appuser/.local/lib/python3.11/site-packages/opentelemetry/util/_decorator.py\", line 71, in async_wrapper\n return await func(*args, **kwargs) # type: ignore\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/ipc/job_proc_lazy_main.py\", line 275, in _traceable_entrypoint\n await self._job_entrypoint_fnc(job_ctx)\n File \"/home/appuser/main.py\", line 1178, in entrypoint\n from livekit.agents.llm import FunctionContext\nImportError: cannot import name 'FunctionContext' from 'livekit.agents.llm' (/home/appuser/.local/lib/python3.11/site-packages/livekit/agents/llm/__init__.py)", "pid": 713, "job_id": "AJ_GhgZ7ohsYagf", "room_id": "RM_7HCL77RftEfV", "timestamp": "2025-11-24T181238.721220+00:00"}
t
Thanks for reaching out, Vaneeza. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're hitting that ImportError because
FunctionContext
was removed in Agents v1.x. Below is what to change and two recommended approaches for passing context (RAG or tools). Acknowledgement • You're asking how to pass function context to an agent for RAG and why
from livekit.agents.llm import FunctionContext
errors. What to do • Remove any import/usage of
FunctionContext
(it's deprecated/removed). • Use one of these approaches: 1) Inject RAG context by overriding
llm_node
(recommended for RAG) • Add your retrieval result into the chat context before the LLM call. • Example pattern:
Copy code
from livekit.agents import Agent, ChatContext, ModelSettings, ChatChunk

class RAGAgent(Agent):
    async def llm_node(self, chat_ctx: ChatContext, tools: list, model_settings: ModelSettings) -> AsyncIterable[ChatChunk]:
        rag_context = await retrieve_rag_context(chat_ctx.messages[-1].text)
        chat_ctx.add_message(role="system", content=rag_context)
        return Agent.default.llm_node(self, chat_ctx, tools, model_settings)
2) Expose functionality via tools using
@function_tool
(recommended for callable functions) • Define tools as methods on your Agent with
@function_tool()
and call them from the agent or LLM. • Example pattern:
Copy code
from livekit.agents import Agent, function_tool

class Assistant(Agent):
    @function_tool()
    async def retrieve_knowledge_base(self, query: str) -> str:
        return await my_rag_function(query)
Key points • Do not import `FunctionContext`; it no longer exists in v1.x. • Use
@function_tool
for callable tools and override
llm_node
to inject external context for RAG. • Ensure your imports use
function_tool
from
livekit.agents.llm
(or
from livekit.agents import function_tool
depending on your code layout). If you want, paste the small code snippet where you currently import/use
FunctionContext
and I’ll show the exact replacement for your case. Sources: Agents v0.x migration guide - Python | LiveKit docs | Agents v0.x migration guide - Node.js | LiveKit Docs | Tool definition and use | LiveKit docs | External data and RAG | LiveKit Docs
p
# Create initial chat context with KB functions if enabled initial_ctx = None if knowledge_base.enabled: from livekit.agents import llm fnc_ctx = llm.FunctionContext() fnc_ctx.ai_callable()(search_knowledge_base) fnc_ctx.ai_callable()(get_knowledge_base_stats)
t
Thanks — I see you’re instantiating
FunctionContext
and registering callables. That API is removed. Replace that pattern with either tool methods (
@function_tool
) or inject RAG context via
llm_node
. Two concise fixes: 1) If you want callable KB functions, define them as tools on the Agent:
Copy code
from livekit.agents import Agent, function_tool

class KBAgent(Agent):
    @function_tool()
    async def search_knowledge_base(self, query: str) -> str:
        return await search_knowledge_base(query)  # your impl

    @function_tool()
    async def get_knowledge_base_stats(self) -> str:
        return await get_knowledge_base_stats()  # your impl
No
FunctionContext
needed — the agent registers these tools automatically. 2) If you just want to inject KB content (RAG) into the prompt, override
llm_node
and add system/context messages:
Copy code
from livekit.agents import Agent, ChatContext, ModelSettings, ChatChunk

class RAGAgent(Agent):
    async def llm_node(self, chat_ctx: ChatContext, tools: list, model_settings: ModelSettings) -> AsyncIterable[ChatChunk]:
        if knowledge_base.enabled:
            rag_context = await retrieve_rag_context(chat_ctx.messages[-1].text)
            chat_ctx.add_message(role="system", content=rag_context)
        return Agent.default.llm_node(self, chat_ctx, tools, model_settings)
If you paste the few lines where you currently call
FunctionContext
I’ll rewrite them directly into one of these patterns for your code.