Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Cohere ai Cohere python Tool Execution Pattern

From Leeroopedia
Metadata Value
Source Cohere Tool Use
Domains Agentic_AI, Function_Calling, Tool_Use
Last Updated 2026-02-15 14:00 GMT
Implements Principle:Cohere_ai_Cohere_python_Tool_Function_Execution

Overview

Interface specification for user-defined tool function execution in agentic chat workflows.

Description

This is a Pattern Doc documenting the user-defined tool execution pattern. Users implement functions matching their tool schemas and a dispatch mechanism to route tool calls to implementations. The pattern includes argument parsing, execution, error handling, and result formatting.

Usage

Implement a tool dispatch function that maps tool names to implementations. Execute after receiving ToolCallV2 objects from the chat API.

Code Reference

Source Location

N/A (user-defined pattern)

Interface Specification

import json
from typing import Any, Callable, Dict

# Pattern: Tool function dispatch
def execute_tool_call(
    function_name: str,
    arguments_json: str,
    tool_registry: Dict[str, Callable[..., Any]],
) -> str:
    """
    Execute a tool call and return the result as a JSON string.

    Args:
        function_name: Name of the function to call
        arguments_json: JSON-encoded arguments string
        tool_registry: Map of function names to callables

    Returns:
        JSON string result for ToolChatMessageV2
    """
    if function_name not in tool_registry:
        return json.dumps({"error": f"Unknown function: {function_name}"})

    try:
        args = json.loads(arguments_json)
        result = tool_registry[function_name](**args)
        return json.dumps(result) if not isinstance(result, str) else result
    except Exception as e:
        return json.dumps({"error": str(e)})

Import

N/A (user-defined)

I/O Contract

Direction Description
Inputs ToolCallV2 with function.name (str) and function.arguments (JSON string)
Outputs Result string (JSON) for ToolChatMessageV2.content

Usage Examples

import json

# Define tool implementations
def get_weather(location: str, unit: str = "celsius") -> dict:
    # In practice, call a weather API
    return {"temperature": 22, "unit": unit, "condition": "sunny"}

def search_database(query: str) -> dict:
    return {"results": ["Result 1", "Result 2"]}

# Tool registry
tools_registry = {
    "get_weather": get_weather,
    "search_database": search_database,
}

# Execute tool calls from model response
for tool_call in response.message.tool_calls:
    args = json.loads(tool_call.function.arguments)
    result = tools_registry[tool_call.function.name](**args)
    tool_results.append({
        "id": tool_call.id,
        "result": json.dumps(result),
    })

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment