Skip to content

What is the recommended way to add image to LLM chat history after calling a tool? #796

Closed

Description

@101scholar

If a tool calling returns an image instead of texts, how to add it properly to the chat history? My workaround is

def load_screenshot(tool_context: ToolContext):
    """ Load the system screenshot from clipboard.
    """
    if image_bytes := get_clipboard_image():
        tool_context._invocation_context.user_content.parts.append(
            types.Part.from_bytes(
                data=image_bytes,
                mime_type="image/png"
            )
        )
        return "Successfully load screenshot from clipboard."
    else:
        return "Failed to load screenshot from clipboard."

Considering _invocation_context is an internal variable, I guess it is not the traditional way to do this. Then what is the recommended way for this purpose?

Activity

  1. locked and limited conversation to collaborators on May 20, 2025
  2. converted this issue into a discussion #809 on May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions