Skip to content

Add utility function for extracting tool usage from Claudette responses #81

Description

@erikgaas

Problem

Claudette provides the contents function to extract text content from responses, but there's no equivalent utility for extracting tool usage information (tool name, arguments, etc.) from responses. This makes it difficult to analyze or log tool usage patterns.

Steps to Reproduce

  1. Use Claudette to make a tool call
  2. Try to extract tool usage information from the response
  3. Need to manually parse the message object to find tool usage

Current Workaround

Currently, users need to manually extract tool usage information like this:

def extract_tool_usage(message):
    if not hasattr(message, 'content') or isinstance(message.content, str):
        return []
    
    tool_uses = [i for i in message.content if getattr(i, 'type', None) == 'tool_use']
    
    result = []
    for tool in tool_uses:
        result.append({
            'name': tool.name,
            'input': tool.input,
            'id': tool.id
        })
    
    return result

Impact

This lack of a built-in utility makes it harder to:

  1. Log and analyze tool usage patterns
  2. Extract tool arguments for debugging
  3. Build higher-level abstractions on top of tool usage

Proposed Solution

Add a utility function similar to contents that extracts tool usage information:

def tool_usage(r):
    """Helper to get tool usage information from Claude response `r`."""
    if not hasattr(r, 'content') or isinstance(r.content, str):
        return []
    
    tool_uses = [i for i in r.content if getattr(i, 'type', None) == 'tool_use']
    
    result = []
    for tool in tool_uses:
        result.append({
            'name': tool.name,
            'input': tool.input,
            'id': tool.id
        })
    
    return result

This would provide a consistent and convenient way to extract tool usage information, similar to how contents works for text content.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions