# Interfaze as Tools

URL: https://interfaze.ai/docs/interfaze-as-tools

You can wire any Interfaze capability directly as a tool inside your existing agent. Your main model decides when to call it, Interfaze runs the task, and the result comes back as clean structured JSON — no extra infrastructure required.

This works by combining [run-task mode](https://interfaze.ai/docs/run-tasks) with your SDK's tool-calling primitives.

## Available tasks

| Task               | What it does                                              |
| ------------------ | --------------------------------------------------------- |
| `ocr`              | Extract text and structured data from images or documents |
| `object_detection` | Detect and locate objects in images                       |
| `web_search`       | Real-time web search with sources                         |
| `scraper`          | Extract structured content from any web page              |
| `speech_to_text`   | Transcribe audio files                                    |
| `translate`        | Translate text between languages                          |

## The helper function

Every example below uses this helper. It calls Interfaze in run-task mode and returns the raw result your agent can reason over.

```tabs
language-typescript

tab-OpenAI SDK
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";

const interfaze = new OpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});

async function runInterfazeTask(task: string, prompt: string) {
  const res = await interfaze.chat.completions.create({
    model: "interfaze-beta",
    messages: [
      { role: "system", content: `<task>${task}</task>` },
      { role: "user", content: prompt },
    ],
    response_format: zodResponseFormat(z.any(), "empty_schema"),
  });
  return JSON.parse(res.choices[0].message.content!).result;
}

tab-Vercel AI SDK
import { createOpenAI } from "@ai-sdk/openai";
import { generateText, Output } from "ai";
import { z } from "zod";

const interfaze = createOpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});

async function runInterfazeTask(task: string, prompt: string) {
  const { output } = await generateText({
    model: interfaze.chat("interfaze-beta"),
    system: `<task>${task}</task>`,
    output: Output.object({ schema: z.any() }),
    prompt,
  });
  return output.result;
}

tab-LangChain SDK
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage, SystemMessage } from "@langchain/core/messages";

const interfaze = new ChatOpenAI({
  openAIApiKey: process.env.INTERFAZE_API_KEY,
  configuration: { baseURL: "https://api.interfaze.ai/v1" },
  modelName: "interfaze-beta",
});

async function runInterfazeTask(taskName: string, prompt: string) {
  const structured = interfaze.withStructuredOutput({});
  const res = await structured.invoke([
    new SystemMessage(`<task>${taskName}</task>`),
    new HumanMessage(prompt),
  ]);
  return res.result;
}

language-python

tab-OpenAI SDK
import json, os
from openai import OpenAI

interfaze = OpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
)

def run_interfaze_task(task: str, prompt: str) -> dict:
    res = interfaze.chat.completions.create(
        model="interfaze-beta",
        response_format={
            "type": "json_schema",
            "json_schema": {
                "name": "empty_schema",
                "schema": {"type": "object", "properties": {}, "additionalProperties": True},
            },
        },
        messages=[
            {"role": "system", "content": f"<task>{task}</task>"},
            {"role": "user", "content": prompt},
        ],
    )
    return json.loads(res.choices[0].message.content)["result"]

tab-LangChain SDK
import json, os
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage

interfaze = ChatOpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
    model="interfaze-beta",
)

def run_interfaze_task(task_name: str, prompt: str) -> dict:
    structured = interfaze.with_structured_output(
        {"name": "empty_schema", "schema": {"type": "object", "properties": {}, "additionalProperties": True}}
    )
    res = structured.invoke([
        SystemMessage(content=f"<task>{task_name}</task>"),
        HumanMessage(content=prompt),
    ])
    return res.get("result", res)
```

## OCR — extract text from documents

Register OCR as a tool. Your agent calls it when it encounters a document or image URL and gets back clean structured text.

```tabs
language-typescript

tab-OpenAI SDK
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";

const interfaze = new OpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});
const model = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function runInterfazeTask(task: string, prompt: string) {
  const res = await interfaze.chat.completions.create({
    model: "interfaze-beta",
    messages: [
      { role: "system", content: `<task>${task}</task>` },
      { role: "user", content: prompt },
    ],
    response_format: zodResponseFormat(z.any(), "empty_schema"),
  });
  return JSON.parse(res.choices[0].message.content!).result;
}

const ocrTool = {
  type: "function" as const,
  function: {
    name: "ocr",
    description: "Extract text and structured data from images or documents.",
    parameters: {
      type: "object",
      properties: {
        url: { type: "string", description: "URL of the image or document." },
      },
      required: ["url"],
    },
  },
};

const messages: OpenAI.ChatCompletionMessageParam[] = [
  {
    role: "user",
    content: "Extract the title, authors, and abstract from: https://arxiv.org/pdf/2602.04101",
  },
];

const res = await model.chat.completions.create({
  model: "gpt-4o",
  messages,
  tools: [ocrTool],
  tool_choice: "auto",
});

messages.push(res.choices[0].message);

for (const call of res.choices[0].message.tool_calls ?? []) {
  const { url } = JSON.parse(call.function.arguments);
  const result = await runInterfazeTask("ocr", `Extract all text and data from: ${url}`);
  messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
}

const final = await model.chat.completions.create({ model: "gpt-4o", messages });
console.log(final.choices[0].message.content);

tab-Vercel AI SDK
import { createOpenAI, openai } from "@ai-sdk/openai";
import { generateText, Output, tool, stepCountIs } from "ai";
import { z } from "zod";

const interfaze = createOpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});

async function runInterfazeTask(task: string, prompt: string) {
  const { output } = await generateText({
    model: interfaze.chat("interfaze-beta"),
    system: `<task>${task}</task>`,
    output: Output.object({ schema: z.any() }),
    prompt,
  });
  return output.result;
}

const { text } = await generateText({
  model: openai("gpt-4o"),
  tools: {
    ocr: tool({
      description: "Extract text and structured data from images or documents.",
      inputSchema: z.object({
        url: z.string().describe("URL of the image or document."),
      }),
      execute: async ({ url }) =>
        runInterfazeTask("ocr", `Extract all text and data from: ${url}`),
    }),
  },
  stopWhen: stepCountIs(3),
  prompt: "Extract the title, authors, and abstract from: https://arxiv.org/pdf/2602.04101",
});

console.log(text);

tab-LangChain SDK
import { ChatOpenAI } from "@langchain/openai";
import { tool } from "@langchain/core/tools";
import { HumanMessage, SystemMessage, ToolMessage } from "@langchain/core/messages";
import { z } from "zod";

const interfaze = new ChatOpenAI({
  openAIApiKey: process.env.INTERFAZE_API_KEY,
  configuration: { baseURL: "https://api.interfaze.ai/v1" },
  modelName: "interfaze-beta",
});

async function runInterfazeTask(taskName: string, prompt: string) {
  const structured = interfaze.withStructuredOutput({});
  const res = await structured.invoke([
    new SystemMessage(`<task>${taskName}</task>`),
    new HumanMessage(prompt),
  ]);
  return res.result;
}

const ocrTool = tool(
  async ({ url }) =>
    JSON.stringify(await runInterfazeTask("ocr", `Extract all text and data from: ${url}`)),
  {
    name: "ocr",
    description: "Extract text and structured data from images or documents.",
    schema: z.object({ url: z.string().describe("URL of the image or document.") }),
  }
);

const model = new ChatOpenAI({ model: "gpt-4o" }).bindTools([ocrTool]);

const messages = [
  new HumanMessage("Extract the title, authors, and abstract from: https://arxiv.org/pdf/2602.04101"),
];

const response = await model.invoke(messages);
messages.push(response);

for (const call of response.tool_calls ?? []) {
  const result = await ocrTool.invoke(call.args);
  messages.push(new ToolMessage({ content: result, tool_call_id: call.id }));
}

const final = await model.invoke(messages);
console.log(final.content);

language-python

tab-OpenAI SDK
import json, os
from openai import OpenAI

interfaze = OpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
)
model = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

def run_interfaze_task(task: str, prompt: str) -> dict:
    res = interfaze.chat.completions.create(
        model="interfaze-beta",
        response_format={
            "type": "json_schema",
            "json_schema": {
                "name": "empty_schema",
                "schema": {"type": "object", "properties": {}, "additionalProperties": True},
            },
        },
        messages=[
            {"role": "system", "content": f"<task>{task}</task>"},
            {"role": "user", "content": prompt},
        ],
    )
    return json.loads(res.choices[0].message.content)["result"]

ocr_tool = {
    "type": "function",
    "function": {
        "name": "ocr",
        "description": "Extract text and structured data from images or documents.",
        "parameters": {
            "type": "object",
            "properties": {"url": {"type": "string", "description": "URL of the image or document."}},
            "required": ["url"],
        },
    },
}

messages = [
    {"role": "user", "content": "Extract the title, authors, and abstract from: https://arxiv.org/pdf/2602.04101"}
]

res = model.chat.completions.create(model="gpt-4o", messages=messages, tools=[ocr_tool], tool_choice="auto")
messages.append(res.choices[0].message)

for call in res.choices[0].message.tool_calls or []:
    args = json.loads(call.function.arguments)
    result = run_interfaze_task("ocr", f"Extract all text and data from: {args['url']}")
    messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})

final = model.chat.completions.create(model="gpt-4o", messages=messages)
print(final.choices[0].message.content)

tab-LangChain SDK
import json, os
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage

interfaze = ChatOpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
    model="interfaze-beta",
)

def run_interfaze_task(task_name: str, prompt: str) -> dict:
    structured = interfaze.with_structured_output(
        {"name": "empty_schema", "schema": {"type": "object", "properties": {}, "additionalProperties": True}}
    )
    res = structured.invoke([
        SystemMessage(content=f"<task>{task_name}</task>"),
        HumanMessage(content=prompt),
    ])
    return res.get("result", res)

@tool
def ocr(url: str) -> str:
    """Extract text and structured data from images or documents."""
    return json.dumps(run_interfaze_task("ocr", f"Extract all text and data from: {url}"))

model = ChatOpenAI(model="gpt-4o").bind_tools([ocr])

messages = [HumanMessage(content="Extract the title, authors, and abstract from: https://arxiv.org/pdf/2602.04101")]
response = model.invoke(messages)
messages.append(response)

for call in response.tool_calls or []:
    result = ocr.invoke(call["args"])
    messages.append(ToolMessage(content=result, tool_call_id=call["id"]))

final = model.invoke(messages)
print(final.content)
```

## Web search — live data at runtime

Give your agent access to real-time information. It can look up current events, check documentation, or verify facts before responding.

```tabs
language-typescript

tab-OpenAI SDK
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";

const interfaze = new OpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});
const model = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function runInterfazeTask(task: string, prompt: string) {
  const res = await interfaze.chat.completions.create({
    model: "interfaze-beta",
    messages: [
      { role: "system", content: `<task>${task}</task>` },
      { role: "user", content: prompt },
    ],
    response_format: zodResponseFormat(z.any(), "empty_schema"),
  });
  return JSON.parse(res.choices[0].message.content!).result;
}

const webSearchTool = {
  type: "function" as const,
  function: {
    name: "web_search",
    description: "Search the web for real-time information.",
    parameters: {
      type: "object",
      properties: {
        query: { type: "string", description: "The search query." },
      },
      required: ["query"],
    },
  },
};

const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: "user", content: "What are the latest updates to the OpenAI API?" },
];

const res = await model.chat.completions.create({
  model: "gpt-4o",
  messages,
  tools: [webSearchTool],
  tool_choice: "auto",
});

messages.push(res.choices[0].message);

for (const call of res.choices[0].message.tool_calls ?? []) {
  const { query } = JSON.parse(call.function.arguments);
  const result = await runInterfazeTask("web_search", query);
  messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
}

const final = await model.chat.completions.create({ model: "gpt-4o", messages });
console.log(final.choices[0].message.content);

tab-Vercel AI SDK
import { createOpenAI, openai } from "@ai-sdk/openai";
import { generateText, Output, tool, stepCountIs } from "ai";
import { z } from "zod";

const interfaze = createOpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});

async function runInterfazeTask(task: string, prompt: string) {
  const { output } = await generateText({
    model: interfaze.chat("interfaze-beta"),
    system: `<task>${task}</task>`,
    output: Output.object({ schema: z.any() }),
    prompt,
  });
  return output.result;
}

const { text } = await generateText({
  model: openai("gpt-4o"),
  tools: {
    web_search: tool({
      description: "Search the web for real-time information.",
      inputSchema: z.object({
        query: z.string().describe("The search query."),
      }),
      execute: async ({ query }) => runInterfazeTask("web_search", query),
    }),
  },
  stopWhen: stepCountIs(3),
  prompt: "What are the latest updates to the OpenAI API?",
});

console.log(text);

tab-LangChain SDK
import { ChatOpenAI } from "@langchain/openai";
import { tool } from "@langchain/core/tools";
import { HumanMessage, SystemMessage, ToolMessage } from "@langchain/core/messages";
import { z } from "zod";

const interfaze = new ChatOpenAI({
  openAIApiKey: process.env.INTERFAZE_API_KEY,
  configuration: { baseURL: "https://api.interfaze.ai/v1" },
  modelName: "interfaze-beta",
});

async function runInterfazeTask(taskName: string, prompt: string) {
  const structured = interfaze.withStructuredOutput({});
  const res = await structured.invoke([
    new SystemMessage(`<task>${taskName}</task>`),
    new HumanMessage(prompt),
  ]);
  return res.result;
}

const webSearchTool = tool(
  async ({ query }) =>
    JSON.stringify(await runInterfazeTask("web_search", query)),
  {
    name: "web_search",
    description: "Search the web for real-time information.",
    schema: z.object({ query: z.string().describe("The search query.") }),
  }
);

const model = new ChatOpenAI({ model: "gpt-4o" }).bindTools([webSearchTool]);

const messages = [
  new HumanMessage("What are the latest updates to the OpenAI API?"),
];

const response = await model.invoke(messages);
messages.push(response);

for (const call of response.tool_calls ?? []) {
  const result = await webSearchTool.invoke(call.args);
  messages.push(new ToolMessage({ content: result, tool_call_id: call.id }));
}

const final = await model.invoke(messages);
console.log(final.content);

language-python

tab-OpenAI SDK
import json, os
from openai import OpenAI

interfaze = OpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
)
model = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

def run_interfaze_task(task: str, prompt: str) -> dict:
    res = interfaze.chat.completions.create(
        model="interfaze-beta",
        response_format={
            "type": "json_schema",
            "json_schema": {
                "name": "empty_schema",
                "schema": {"type": "object", "properties": {}, "additionalProperties": True},
            },
        },
        messages=[
            {"role": "system", "content": f"<task>{task}</task>"},
            {"role": "user", "content": prompt},
        ],
    )
    return json.loads(res.choices[0].message.content)["result"]

web_search_tool = {
    "type": "function",
    "function": {
        "name": "web_search",
        "description": "Search the web for real-time information.",
        "parameters": {
            "type": "object",
            "properties": {"query": {"type": "string", "description": "The search query."}},
            "required": ["query"],
        },
    },
}

messages = [{"role": "user", "content": "What are the latest updates to the OpenAI API?"}]

res = model.chat.completions.create(model="gpt-4o", messages=messages, tools=[web_search_tool], tool_choice="auto")
messages.append(res.choices[0].message)

for call in res.choices[0].message.tool_calls or []:
    args = json.loads(call.function.arguments)
    result = run_interfaze_task("web_search", args["query"])
    messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})

final = model.chat.completions.create(model="gpt-4o", messages=messages)
print(final.choices[0].message.content)

tab-LangChain SDK
import json, os
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage

interfaze = ChatOpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
    model="interfaze-beta",
)

def run_interfaze_task(task_name: str, prompt: str) -> dict:
    structured = interfaze.with_structured_output(
        {"name": "empty_schema", "schema": {"type": "object", "properties": {}, "additionalProperties": True}}
    )
    res = structured.invoke([
        SystemMessage(content=f"<task>{task_name}</task>"),
        HumanMessage(content=prompt),
    ])
    return res.get("result", res)

@tool
def web_search(query: str) -> str:
    """Search the web for real-time information."""
    return json.dumps(run_interfaze_task("web_search", query))

model = ChatOpenAI(model="gpt-4o").bind_tools([web_search])

messages = [HumanMessage(content="What are the latest updates to the OpenAI API?")]
response = model.invoke(messages)
messages.append(response)

for call in response.tool_calls or []:
    result = web_search.invoke(call["args"])
    messages.append(ToolMessage(content=result, tool_call_id=call["id"]))

final = model.invoke(messages)
print(final.content)
```

## Speech-to-text — transcribe audio files

Register speech-to-text as a tool. Pass any audio URL and your agent gets a full transcript to summarize, extract action items from, or reason over.

```tabs
language-typescript

tab-OpenAI SDK
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";

const interfaze = new OpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});
const model = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function runInterfazeTask(task: string, prompt: string) {
  const res = await interfaze.chat.completions.create({
    model: "interfaze-beta",
    messages: [
      { role: "system", content: `<task>${task}</task>` },
      { role: "user", content: prompt },
    ],
    response_format: zodResponseFormat(z.any(), "empty_schema"),
  });
  return JSON.parse(res.choices[0].message.content!).result;
}

const sttTool = {
  type: "function" as const,
  function: {
    name: "speech_to_text",
    description: "Transcribe an audio file to text.",
    parameters: {
      type: "object",
      properties: {
        audio_url: { type: "string", description: "URL of the audio file." },
      },
      required: ["audio_url"],
    },
  },
};

const messages: OpenAI.ChatCompletionMessageParam[] = [
  {
    role: "user",
    content: "Summarize this call and list any follow-up items: https://r2public.jigsawstack.com/interfaze/examples/stt_call.mp3",
  },
];

const res = await model.chat.completions.create({
  model: "gpt-4o",
  messages,
  tools: [sttTool],
  tool_choice: "auto",
});

messages.push(res.choices[0].message);

for (const call of res.choices[0].message.tool_calls ?? []) {
  const { audio_url } = JSON.parse(call.function.arguments);
  const result = await runInterfazeTask("speech_to_text", `Transcribe this audio: ${audio_url}`);
  messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
}

const final = await model.chat.completions.create({ model: "gpt-4o", messages });
console.log(final.choices[0].message.content);

tab-Vercel AI SDK
import { createOpenAI, openai } from "@ai-sdk/openai";
import { generateText, Output, tool, stepCountIs } from "ai";
import { z } from "zod";

const interfaze = createOpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});

async function runInterfazeTask(task: string, prompt: string) {
  const { output } = await generateText({
    model: interfaze.chat("interfaze-beta"),
    system: `<task>${task}</task>`,
    output: Output.object({ schema: z.any() }),
    prompt,
  });
  return output.result;
}

const { text } = await generateText({
  model: openai("gpt-4o"),
  tools: {
    speech_to_text: tool({
      description: "Transcribe an audio file to text.",
      inputSchema: z.object({
        audio_url: z.string().describe("URL of the audio file."),
      }),
      execute: async ({ audio_url }) =>
        runInterfazeTask("speech_to_text", `Transcribe this audio: ${audio_url}`),
    }),
  },
  stopWhen: stepCountIs(3),
  prompt: "Summarize this call and list any follow-up items: https://r2public.jigsawstack.com/interfaze/examples/stt_call.mp3",
});

console.log(text);

tab-LangChain SDK
import { ChatOpenAI } from "@langchain/openai";
import { tool } from "@langchain/core/tools";
import { HumanMessage, SystemMessage, ToolMessage } from "@langchain/core/messages";
import { z } from "zod";

const interfaze = new ChatOpenAI({
  openAIApiKey: process.env.INTERFAZE_API_KEY,
  configuration: { baseURL: "https://api.interfaze.ai/v1" },
  modelName: "interfaze-beta",
});

async function runInterfazeTask(taskName: string, prompt: string) {
  const structured = interfaze.withStructuredOutput({});
  const res = await structured.invoke([
    new SystemMessage(`<task>${taskName}</task>`),
    new HumanMessage(prompt),
  ]);
  return res.result;
}

const sttTool = tool(
  async ({ audio_url }) =>
    JSON.stringify(
      await runInterfazeTask("speech_to_text", `Transcribe this audio: ${audio_url}`)
    ),
  {
    name: "speech_to_text",
    description: "Transcribe an audio file to text.",
    schema: z.object({ audio_url: z.string().describe("URL of the audio file.") }),
  }
);

const model = new ChatOpenAI({ model: "gpt-4o" }).bindTools([sttTool]);

const messages = [
  new HumanMessage("Summarize this call and list any follow-up items: https://r2public.jigsawstack.com/interfaze/examples/stt_call.mp3"),
];

const response = await model.invoke(messages);
messages.push(response);

for (const call of response.tool_calls ?? []) {
  const result = await sttTool.invoke(call.args);
  messages.push(new ToolMessage({ content: result, tool_call_id: call.id }));
}

const final = await model.invoke(messages);
console.log(final.content);

language-python

tab-OpenAI SDK
import json, os
from openai import OpenAI

interfaze = OpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
)
model = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

def run_interfaze_task(task: str, prompt: str) -> dict:
    res = interfaze.chat.completions.create(
        model="interfaze-beta",
        response_format={
            "type": "json_schema",
            "json_schema": {
                "name": "empty_schema",
                "schema": {"type": "object", "properties": {}, "additionalProperties": True},
            },
        },
        messages=[
            {"role": "system", "content": f"<task>{task}</task>"},
            {"role": "user", "content": prompt},
        ],
    )
    return json.loads(res.choices[0].message.content)["result"]

stt_tool = {
    "type": "function",
    "function": {
        "name": "speech_to_text",
        "description": "Transcribe an audio file to text.",
        "parameters": {
            "type": "object",
            "properties": {"audio_url": {"type": "string", "description": "URL of the audio file."}},
            "required": ["audio_url"],
        },
    },
}

messages = [
    {
        "role": "user",
        "content": "Summarize this call and list any follow-up items: https://r2public.jigsawstack.com/interfaze/examples/stt_call.mp3",
    }
]

res = model.chat.completions.create(model="gpt-4o", messages=messages, tools=[stt_tool], tool_choice="auto")
messages.append(res.choices[0].message)

for call in res.choices[0].message.tool_calls or []:
    args = json.loads(call.function.arguments)
    result = run_interfaze_task("speech_to_text", f"Transcribe this audio: {args['audio_url']}")
    messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})

final = model.chat.completions.create(model="gpt-4o", messages=messages)
print(final.choices[0].message.content)

tab-LangChain SDK
import json, os
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage

interfaze = ChatOpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
    model="interfaze-beta",
)

def run_interfaze_task(task_name: str, prompt: str) -> dict:
    structured = interfaze.with_structured_output(
        {"name": "empty_schema", "schema": {"type": "object", "properties": {}, "additionalProperties": True}}
    )
    res = structured.invoke([
        SystemMessage(content=f"<task>{task_name}</task>"),
        HumanMessage(content=prompt),
    ])
    return res.get("result", res)

@tool
def speech_to_text(audio_url: str) -> str:
    """Transcribe an audio file to text."""
    return json.dumps(run_interfaze_task("speech_to_text", f"Transcribe this audio: {audio_url}"))

model = ChatOpenAI(model="gpt-4o").bind_tools([speech_to_text])

messages = [HumanMessage(content="Summarize this call and list any follow-up items: https://r2public.jigsawstack.com/interfaze/examples/stt_call.mp3")]
response = model.invoke(messages)
messages.append(response)

for call in response.tool_calls or []:
    result = speech_to_text.invoke(call["args"])
    messages.append(ToolMessage(content=result, tool_call_id=call["id"]))

final = model.invoke(messages)
print(final.content)
```

## Combining multiple tools

You can register any combination of tasks as tools. The agent picks whichever it needs based on the request.

```tabs
language-typescript

tab-OpenAI SDK
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";

const interfaze = new OpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});
const model = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function runInterfazeTask(task: string, prompt: string) {
  const res = await interfaze.chat.completions.create({
    model: "interfaze-beta",
    messages: [
      { role: "system", content: `<task>${task}</task>` },
      { role: "user", content: prompt },
    ],
    response_format: zodResponseFormat(z.any(), "empty_schema"),
  });
  return JSON.parse(res.choices[0].message.content!).result;
}

const tools: OpenAI.ChatCompletionTool[] = [
  {
    type: "function",
    function: {
      name: "ocr",
      description: "Extract text and structured data from images or documents.",
      parameters: {
        type: "object",
        properties: { url: { type: "string" } },
        required: ["url"],
      },
    },
  },
  {
    type: "function",
    function: {
      name: "web_search",
      description: "Search the web for real-time information.",
      parameters: {
        type: "object",
        properties: { query: { type: "string" } },
        required: ["query"],
      },
    },
  },
  {
    type: "function",
    function: {
      name: "speech_to_text",
      description: "Transcribe an audio file to text.",
      parameters: {
        type: "object",
        properties: { audio_url: { type: "string" } },
        required: ["audio_url"],
      },
    },
  },
];

const taskMap: Record<string, (args: Record<string, string>) => Promise<unknown>> = {
  ocr: ({ url }) => runInterfazeTask("ocr", `Extract all text from: ${url}`),
  web_search: ({ query }) => runInterfazeTask("web_search", query),
  speech_to_text: ({ audio_url }) => runInterfazeTask("speech_to_text", `Transcribe: ${audio_url}`),
};

const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: "user", content: "Search for the latest news on AI agents and summarize the top results." },
];

let res = await model.chat.completions.create({ model: "gpt-4o", messages, tools, tool_choice: "auto" });
messages.push(res.choices[0].message);

while (res.choices[0].message.tool_calls?.length) {
  for (const call of res.choices[0].message.tool_calls) {
    const args = JSON.parse(call.function.arguments);
    const result = await taskMap[call.function.name](args);
    messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
  }
  res = await model.chat.completions.create({ model: "gpt-4o", messages, tools, tool_choice: "auto" });
  messages.push(res.choices[0].message);
}

console.log(res.choices[0].message.content);

tab-Vercel AI SDK
import { createOpenAI, openai } from "@ai-sdk/openai";
import { generateText, Output, tool, stepCountIs } from "ai";
import { z } from "zod";

const interfaze = createOpenAI({
  apiKey: process.env.INTERFAZE_API_KEY,
  baseURL: "https://api.interfaze.ai/v1",
});

async function runInterfazeTask(task: string, prompt: string) {
  const { output } = await generateText({
    model: interfaze.chat("interfaze-beta"),
    system: `<task>${task}</task>`,
    output: Output.object({ schema: z.any() }),
    prompt,
  });
  return output.result;
}

const { text } = await generateText({
  model: openai("gpt-4o"),
  tools: {
    ocr: tool({
      description: "Extract text and structured data from images or documents.",
      inputSchema: z.object({ url: z.string() }),
      execute: async ({ url }) => runInterfazeTask("ocr", `Extract all text from: ${url}`),
    }),
    web_search: tool({
      description: "Search the web for real-time information.",
      inputSchema: z.object({ query: z.string() }),
      execute: async ({ query }) => runInterfazeTask("web_search", query),
    }),
    speech_to_text: tool({
      description: "Transcribe an audio file to text.",
      inputSchema: z.object({ audio_url: z.string() }),
      execute: async ({ audio_url }) =>
        runInterfazeTask("speech_to_text", `Transcribe: ${audio_url}`),
    }),
  },
  stopWhen: stepCountIs(5),
  prompt: "Search for the latest news on AI agents and summarize the top results.",
});

console.log(text);

language-python

tab-OpenAI SDK
import json, os
from openai import OpenAI

interfaze = OpenAI(
    api_key=os.getenv("INTERFAZE_API_KEY"),
    base_url="https://api.interfaze.ai/v1",
)
model = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

def run_interfaze_task(task: str, prompt: str) -> dict:
    res = interfaze.chat.completions.create(
        model="interfaze-beta",
        response_format={
            "type": "json_schema",
            "json_schema": {
                "name": "empty_schema",
                "schema": {"type": "object", "properties": {}, "additionalProperties": True},
            },
        },
        messages=[
            {"role": "system", "content": f"<task>{task}</task>"},
            {"role": "user", "content": prompt},
        ],
    )
    return json.loads(res.choices[0].message.content)["result"]

tools = [
    {
        "type": "function",
        "function": {
            "name": "ocr",
            "description": "Extract text and structured data from images or documents.",
            "parameters": {"type": "object", "properties": {"url": {"type": "string"}}, "required": ["url"]},
        },
    },
    {
        "type": "function",
        "function": {
            "name": "web_search",
            "description": "Search the web for real-time information.",
            "parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]},
        },
    },
    {
        "type": "function",
        "function": {
            "name": "speech_to_text",
            "description": "Transcribe an audio file to text.",
            "parameters": {"type": "object", "properties": {"audio_url": {"type": "string"}}, "required": ["audio_url"]},
        },
    },
]

task_map = {
    "ocr": lambda args: run_interfaze_task("ocr", f"Extract all text from: {args['url']}"),
    "web_search": lambda args: run_interfaze_task("web_search", args["query"]),
    "speech_to_text": lambda args: run_interfaze_task("speech_to_text", f"Transcribe: {args['audio_url']}"),
}

messages = [{"role": "user", "content": "Search for the latest news on AI agents and summarize the top results."}]

res = model.chat.completions.create(model="gpt-4o", messages=messages, tools=tools, tool_choice="auto")
messages.append(res.choices[0].message)

while res.choices[0].message.tool_calls:
    for call in res.choices[0].message.tool_calls:
        args = json.loads(call.function.arguments)
        result = task_map[call.function.name](args)
        messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
    res = model.chat.completions.create(model="gpt-4o", messages=messages, tools=tools, tool_choice="auto")
    messages.append(res.choices[0].message)

print(res.choices[0].message.content)
```
