can i return image content as function tool output...
# ask-ai
e
can i return image content as function tool output ?
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking whether a function tool can return image content. Yes — while a tool's return value is typically string/structured data, you can surface images to the chat by adding an
ImageContent
message to the chat context. To do this: 1. Create or copy the current
ChatContext
. 2. Use
chat_ctx.add_message()
with role "assistant" and include an
ImageContent
object whose
image
is a data URL (e.g.,
data:image/png;base64,...
) or an external image URL. 3. Update the agent with the modified chat context so the image appears in the conversation. Example (Python):
Copy code
from livekit.agents.llm import ImageContent, ChatContext

chat_ctx = self.chat_ctx.copy()
chat_ctx.add_message(
    role="assistant",
    content=[
        "Here is an image:",
        ImageContent(image=f"data:image/png;base64,{base64_encoded_image}")
    ]
)
await self.update_chat_ctx(chat_ctx)
Would you like an example for returning an externally hosted image URL instead of a base64 data URL? Sources: Vision | LiveKit Docs
e
if i return something from the tool call, will that also get added to the chat ?
t
You're asking whether a tool's return value is also added to the chat. Yes — the tool output is converted to a
FunctionCallOutput
(stringified if needed) and is automatically appended to the chat context. The LLM receives that output and can use it for its next reply or to trigger further tool calls. If a tool returns
None
or nothing, it completes silently and no
FunctionCallOutput
is added. Would you like an example of how a dictionary or image-returning tool appears in the chat context? Sources: Tool definition and use | LiveKit docs
e
yes
t
Great — here are two concise examples showing how a tool return appears in the chat and how to surface an image via the chat context. 1) Tool returns structured data (dictionary) • Tool implementation returns a dict:
Copy code
return {"weather": "sunny", "temperature_f": 70}
• The agent runtime converts that return to a
FunctionCallOutput
(stringified) and appends it to the chat history, e.g.: Assistant (tool output): "{'weather': 'sunny', 'temperature_f': 70}" The LLM then sees that output and can generate a follow-up reply referencing those values. 2) Tool returns image content visible to the user • Return value can be any string/structure, but to show an actual image in the conversation you must add an
ImageContent
message to the chat context:
Copy code
from livekit.agents.llm import ImageContent, ChatContext

# inside the tool/agent code after producing the image bytes/base64
base64_image = "<base64_encoded_png>"

chat_ctx = self.chat_ctx.copy()
chat_ctx.add_message(
    role="assistant",
    content=[
        "Here is the generated image:",
        ImageContent(image=f"data:image/png;base64,{base64_image}")
    ]
)
await self.update_chat_ctx(chat_ctx)

# You can still return structured data if you want:
return {"image_id": "img_123", "note": "image added to chat context"}
• Result in chat: Assistant (tool output): "{'image_id': 'img_123', 'note': 'image added to chat context'}" Assistant (message with image): [renders the image from ImageContent] Notes: • The tool return is always added to the chat as a
FunctionCallOutput
(stringified) unless it returns
None
. • To display an image to the user, explicitly add an
ImageContent
message to the chat context (data URL or external URL). Would you like a variant that uses an external image URL instead of base64?