Skip to main content

Overview

Instead of calling a tool 50 times (e.g., web_search, grep, update_row), the LLM calls batch once with 50 inputs. Ray executes them in parallel and returns one structured result.

Usage

from rayai import tool, BatchTool

@tool(desc="Look up information about a topic")
def lookup_topic(topic: str) -> str:
    return f"Information about {topic}"

@tool(desc="Translate text")
def translate(text: str, target_lang: str) -> str:
    return f"Translated '{text}' to {target_lang}"

# Create batch tool with your tools
batch_tool = BatchTool(tools=[lookup_topic, translate])

# LLM calls it like:
result = batch_tool(
    tool_name="lookup_topic",
    tool_inputs=[
        {"topic": "Python"},
        {"topic": "Rust"},
        {"topic": "Go"}
    ]
)

# Returns:
# {
#     "results": ["Information about Python", "Information about Rust", "Information about Go"],
#     "errors": [None, None, None],
#     "tool_name": "lookup_topic",
#     "count": 3
# }

With LangChain Agent

from langchain.agents import create_agent

from rayai import tool, BatchTool
from rayai.adapters import RayToolWrapper, AgentFramework

@tool(desc="Search Wikipedia")
def search_wiki(query: str) -> str:
    # Your implementation
    return f"Results for {query}"

# Create batch tool
batch_tool = BatchTool(tools=[search_wiki])

# Convert to LangChain format
wrapper = RayToolWrapper(framework=AgentFramework.LANGCHAIN)
[lc_batch_tool] = wrapper.wrap_tools([batch_tool])

# Create LangChain agent (requires OPENAI_API_KEY env var)
agent = create_agent(model="openai:gpt-4o-mini", tools=[lc_batch_tool])

Input & Output

BatchToolInput (what the LLM sends):
FieldTypeDescription
tool_namestrName of tool to call
tool_inputslist[dict[str, Any]]List of input dicts
BatchToolOutput (what gets returned):
FieldTypeDescription
resultslist[Any]Results (None if error)
errorslist[str | None]Error messages (None if success)
tool_namestrName of tool that was called
countintNumber of inputs processed

API Reference

BatchTool

ParameterTypeDescription
toolslist[Callable | RayTool] | NoneList of tools to register
namestrTool name (default: “batch”)
descriptionstrCustom description
validate_inputsboolValidate inputs against schema (default: True)
BatchTool handles partial failures gracefully. If some inputs fail, others still return results. Check the errors list to see which inputs failed.