Registry / ai-ml / function-json-schema

function-json-schema

JSON →
library1.0.4jsnpmunverified

Provides TypeScript types and Zod-based validation for JSON Schema objects used in OpenAI function calling and assistants APIs. Version 1.0.4 (latest). Includes nonstandard extensions like mandatory description, array parameters, returns schema, and usage examples. Differentiates from vanilla JSON Schema by adding OpenAIs nonstandard function schema with coercive validation.

npm install function-json-schema
INSTALL
IMPORT
SIG · FUNCTION-JSON-SCHE
F
function-json-schema
ai-mljavascriptv1.0.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FunctionJson
import { FunctionJson } from 'function-json-schema'
const FunctionJson = require('function-json-schema')
TypeScript type; ESM import only.
functionJsonSchema
import { functionJsonSchema } from 'function-json-schema'
import { FunctionJsonSchema } from 'function-json-schema'
Case-sensitive named export.
coerceFunctionJsonSchema
import { coerceFunctionJsonSchema } from 'function-json-schema'
import { coerceFunctionJsonSchema } from 'function-json-schema/coerce'
Directly from package root.

Shows both strict validation and coercive validation of a function JSON schema.

import { functionJsonSchema, coerceFunctionJsonSchema } from 'function-json-schema'; const rawSchema = { name: 'getWeather', description: 'Gets weather', parameters: { type: 'object', properties: { location: { type: 'string', description: 'city' } }, required: ['location'] }, returns: { type: 'object', properties: { temp: { type: 'number', description: 'temp' } } }, }; // Strict validation const result = functionJsonSchema.safeParse(rawSchema); console.log(result.success); // true // Coercive validation (fills defaults, coerces types) const coerced = coerceFunctionJsonSchema(rawSchema); console.log(coerced); // validated & modified schema
Debug
Known issues
breakingThe package uses nonstandard JSON Schema extensions not compatible with standard JSON Schema validators.
fix
Only use this schema with OpenAI-compatible APIs, not generic JSON Schema tools.
affects: all
gotchaThe parameters field can be an array (represents argument lists) instead of an object with properties. This differs from standard OpenAI function calling schema.
fix
Ensure your LLM API supports array parameters.
affects: all
gotchaThe description field is mandatory on the function object, unlike standard JSON Schema where it's optional.
fix
Always provide a description string.
affects: all
gotchaThe returns field is mandatory, representing the return value schema. Standard OpenAI schemas do not have this field.
fix
Include a returns schema or use coerceFunctionJsonSchema to auto-fill.
affects: all
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'safeParse')
Missing import of functionJsonSchema or using default import incorrectly.
fix
Use named import: import { functionJsonSchema } from 'function-json-schema';
Uncaught TypeError: coerceFunctionJsonSchema is not a function
Package not installed or wrong import path.
fix
Run npm install function-json-schema and use named import.
TypeError: object is not a valid value for type 'object'
Provided schema does not match the FunctionJson type due to missing required fields (description, returns).
fix
Ensure schema includes all required fields: name, description, parameters, returns.
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies
zodrequiredRuntime dependency for schema validation and type inference
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
function-json-schema — npm install function-json-schema · libregistry