Implementation:Openai Openai node RunnableFunction
| Knowledge Sources | |
|---|---|
| Domains | SDK, Tool Calling, Type System |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
RunnableFunction defines the TypeScript type system for runnable tool functions used in the SDK's automatic tool-calling runners.
Description
The RunnableFunction module provides a comprehensive set of types that describe executable function tools for use with ChatCompletionRunner and ChatCompletionStreamingRunner. The type system distinguishes between two variants: RunnableFunctionWithParse<Args>, which includes a parse function to deserialize raw string arguments into a typed Args object before passing them to the main function, and RunnableFunctionWithoutParse, which passes the raw string arguments directly.
Both variants require a function callback that receives the (parsed or raw) arguments and the current runner instance, a parameters field containing a JSON Schema description of the function's input, a description string for the model, and optional name and strict fields. The conditional type RunnableFunction<Args> dispatches between the two variants based on whether Args extends string or object.
The module also provides tool-level wrappers (RunnableToolFunction, RunnableToolFunctionWithParse, RunnableToolFunctionWithoutParse) that add the type: 'function' discriminator, the tuple types RunnableFunctions and RunnableTools for typed arrays of tools, the isRunnableFunctionWithParse type guard, and the ParsingToolFunction helper class for constructing type-safe tool functions.
Usage
Use these types when defining tools for the runTools method on chat completion runners. The RunnableFunctionWithParse variant is ideal when combined with Zod schemas or other parsers to get typed function arguments. The RunnableFunctionWithoutParse variant is suitable when you handle JSON parsing yourself within the function body.
Code Reference
Source Location
- Repository: openai-node
- File: src/lib/RunnableFunction.ts
Signature
export type RunnableFunctionWithParse<Args extends object> = {
function: (
args: Args,
runner: ChatCompletionRunner<unknown> | ChatCompletionStreamingRunner<unknown>,
) => PromiseOrValue<unknown>;
parse: (input: string) => PromiseOrValue<Args>;
parameters: JSONSchema;
description: string;
name?: string | undefined;
strict?: boolean | undefined;
};
export type RunnableFunctionWithoutParse = {
function: (
args: string,
runner: ChatCompletionRunner<unknown> | ChatCompletionStreamingRunner<unknown>,
) => PromiseOrValue<unknown>;
parameters: JSONSchema;
description: string;
name?: string | undefined;
strict?: boolean | undefined;
};
export type RunnableFunction<Args extends object | string> =
Args extends string ? RunnableFunctionWithoutParse
: Args extends object ? RunnableFunctionWithParse<Args>
: never;
export type RunnableToolFunction<Args extends object | string> = { type: 'function'; function: RunnableFunction<Args> };
export type RunnableTools<FunctionsArgs extends BaseFunctionsArgs> = ...;
export function isRunnableFunctionWithParse<Args extends object>(fn: any): fn is RunnableFunctionWithParse<Args>;
export class ParsingToolFunction<Args extends object> {
type: 'function';
function: RunnableFunctionWithParse<Args>;
constructor(input: RunnableFunctionWithParse<Args>);
}
Import
import type {
RunnableFunction,
RunnableFunctionWithParse,
RunnableFunctionWithoutParse,
RunnableToolFunction,
RunnableTools,
} from 'openai/lib/RunnableFunction';
import { ParsingToolFunction, isRunnableFunctionWithParse } from 'openai/lib/RunnableFunction';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| args | string | Yes | The function arguments, either pre-parsed (typed Args) or raw string from the OpenAI API.
|
| runner | ChatCompletionStreamingRunner | Yes | The runner instance executing the tool call, enabling access to conversation state. |
| parameters | JSONSchema |
Yes | JSON Schema describing the function's input parameters for the model. |
| description | string |
Yes | Human-readable description of the function for the model to understand when to call it. |
| parse | (input: string) => Args |
No (only for WithParse) | Deserializer that converts raw JSON string arguments into the typed Args object.
|
Outputs
| Name | Type | Description |
|---|---|---|
| function return | PromiseOrValue<unknown> |
The tool function's return value, which the runner serializes and sends back to the model as the tool call result. |
Usage Examples
import OpenAI from 'openai';
import { z } from 'zod';
import type { RunnableToolFunctionWithParse } from 'openai/lib/RunnableFunction';
const WeatherArgs = z.object({ location: z.string() });
const weatherTool: RunnableToolFunctionWithParse<z.infer<typeof WeatherArgs>> = {
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: {
type: 'object',
properties: { location: { type: 'string' } },
required: ['location'],
},
parse: (input: string) => WeatherArgs.parse(JSON.parse(input)),
function: async (args) => {
return JSON.stringify({ temperature: 72, condition: 'sunny', location: args.location });
},
},
};