Skip to content
reaatechREAATECH

@reaatech/structured-function-calling-ai-adapter-openai

pending npm

Normalizes tool definitions and execution results between a provider-agnostic format and OpenAI’s specific function-calling schema. It provides utility functions to parse JSON arguments, serialize results, and map tool definitions, requiring the `@reaatech/structured-function-calling-ai-core` package as a dependency.

@reaatech/structured-function-calling-ai-adapter-openai

Normalize tool calls to and from OpenAI’s function-calling format.

Installation

terminal
pnpm add @reaatech/structured-function-calling-ai-adapter-openai

API

toToolCall

typescript
function toToolCall(fc: OpenAiFunctionCall): ToolCall;

Parses an OpenAI ChatCompletionMessageFunctionCall-shaped object into a provider-agnostic ToolCall. Parses fc.arguments as JSON; throws if the string is not valid JSON.

OpenAiFunctionCall is { id?: string | null; name?: string | null; arguments?: string | null } — all fields are nullable to match OpenAI API variations.

fromToolCallResult

typescript
function fromToolCallResult(tc: ToolCall, result: unknown): OpenAiToolResult;

Converts a ToolCall and execution result into OpenAI’s ChatCompletionRequestMessageToolResult shape. Serializes result with JSON.stringify.

OpenAiToolResult is { tool_call_id: string; output: string }.

toOpenAiTools

typescript
function toOpenAiTools(defs: readonly ToolDefinition[]): OpenAiTool[];

Converts an array of ToolDefinitions to OpenAI ChatCompletionTool objects. Uses convertToJsonSchema with target: 'openAi' for schema conversion.

OpenAiTool is { type: 'function'; function: { name: string; description: string; parameters: Record<string, unknown> } }.

Types

typescript
type ToolCall = { id: string; name: string; arguments: Record<string, unknown> };
type OpenAiFunctionCall = { id?: string | null; name?: string | null; arguments?: string | null };
type OpenAiToolResult = { tool_call_id: string; output: string };
type OpenAiTool = { type: 'function'; function: { name: string; description: string; parameters: Record<string, unknown> } };

Usage example

typescript
import { toToolCall, fromToolCallResult, toOpenAiTools } from '@reaatech/structured-function-calling-ai-adapter-openai';
import { ToolRegistry } from '@reaatech/structured-function-calling-ai-core';
 
// Convert from OpenAI format
const toolCall = toToolCall({
  id: 'call_abc',
  name: 'get_weather',
  arguments: '{"city":"NYC","units":"metric"}',
});
// → { id: 'call_abc', name: 'get_weather', arguments: { city: 'NYC', units: 'metric' } }
 
// Convert tool definitions to OpenAI tools
const registry = new ToolRegistry();
registry.register({ name: 'get_weather', description: 'Get weather', schema, execute: async () => {} });
const openAiTools = toOpenAiTools(registry.list());
// → [{ type: 'function', function: { name: 'get_weather', ... } }]
 
// Convert result back to OpenAI format
const result = fromToolCallResult(toolCall, { temp: 22, conditions: 'sunny' });
// → { tool_call_id: 'call_abc', output: '{"temp":22,"conditions":"sunny"}' }

Dependencies

  • @reaatech/structured-function-calling-ai-core (workspace:*)