Skip to content

Update tool result handling to support rich content types (images, PDFs) #161

@gadenbuie

Description

@gadenbuie

Background

The ellmer package recently added support for tool results that return non-JSON content types like images and PDFs (ellmer PR #735).

Problem

Currently, shinychat tries to convert tool results to JSON strings using tool_string(), which fails when the tool result contains S7 content types like content_image() or content_pdf().

Proposed Solution

Modify the tool result handling to:

  • Detect S7 content types
  • Render these content types appropriately in the chat interface
  • Fallback to existing JSON conversion for other content types

Example Scenario

library(ellmer)

# Tool that returns an image
cat_image_tool <- tool(
  function() {
    # Simulate generating a cat image
    tmpf <- tempfile(fileext = ".jpg")
    download.file("[https://placekitten.com/200/300](vscode-file://vscode-app/Applications/Positron.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html#)", tmpf, quiet = 
TRUE)
    content_image_file(tmpf)
  },
  name = "get_cat_image",
  description = "Generate a random cat image"
)

# Create a chat
chat <- chat_openai()
chat$register_tool(cat_image_tool)

shinychat::chat_app(chat)

Related Work

/cc @gadenbuie

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