Skip to content

TypeScript Runtime

The TypeScript runtime provides a strongly-typed interface for working with AgentSchema definitions in Node.js and browser environments.

Terminal window
npm install agentschema
# or
yarn add agentschema
# or
pnpm add agentschema
  • Node.js >= 18.0.0
  • TypeScript >= 5.0 (for TypeScript users)

Load agent definitions from YAML or JSON:

import { AgentDefinition } from "agentschema";
// Load from YAML
const agent = AgentDefinition.fromYaml(`
name: my-agent
model: gpt-4o
instructions: You are a helpful assistant.
`);
// Load from JSON
const jsonAgent = AgentDefinition.fromJson(`{
"name": "my-agent",
"model": "gpt-4o",
"instructions": "You are a helpful assistant."
}`);
console.log(agent.name); // "my-agent"
console.log(agent.model); // "gpt-4o"

Build agent definitions using TypeScript classes:

import { AgentDefinition, FunctionTool, Property } from "agentschema";
const agent = new AgentDefinition({
name: "weather-agent",
model: "gpt-4o",
instructions: "You help users check the weather.",
tools: [
new FunctionTool({
name: "get_weather",
description: "Get current weather for a location",
parameters: {
location: new Property({
kind: "string",
description: "City name",
required: true,
}),
},
}),
],
});

Export to YAML or JSON:

// Convert to YAML
const yaml = agent.toYaml();
console.log(yaml);
// Convert to JSON (with indentation)
const json = agent.toJson(undefined, 2);
console.log(json);
// Convert to dictionary
const data = agent.save();

Customize how data is loaded:

import { AgentDefinition, LoadContext } from "agentschema";
const context = new LoadContext({
preProcess: (data) => {
// Transform input before loading
console.log("Loading:", data["name"]);
return data;
},
postProcess: (result) => {
// Transform result after loading
return result;
},
});
const agent = AgentDefinition.fromYaml(yamlContent, context);

Control output formatting:

import { AgentDefinition, SaveContext } from "agentschema";
const context = new SaveContext({
collectionFormat: "object", // Use object format for named collections
useShorthand: true, // Use shorthand notation when possible
});
const yaml = agent.toYaml(context);

The SDK exports all AgentSchema types as TypeScript classes:

ClassDescription
AgentDefinitionComplete agent specification
AgentManifestParameterized agent template
ModelAI model configuration
ModelOptionsModel parameters (temperature, tokens, etc.)
ClassDescription
FunctionToolCustom function definitions
OpenApiToolOpenAPI/Swagger integrations
McpToolModel Context Protocol tools
CodeInterpreterToolCode execution capability
FileSearchToolFile search capability
WebSearchToolWeb search capability
CustomToolCustom tool implementations
ClassDescription
ApiKeyConnectionAPI key authentication
AnonymousConnectionNo authentication
ReferenceConnectionReference to external connection
RemoteConnectionRemote service connection
ClassDescription
PropertyBase property type
ObjectPropertyObject/nested property
ArrayPropertyArray/list property
PropertySchemaSchema with multiple properties

The SDK provides full TypeScript support with:

  • Complete type definitions for all classes
  • IntelliSense/autocomplete in VS Code
  • Compile-time type checking
  • JSDoc documentation
import { AgentDefinition } from "agentschema";
// TypeScript knows all available properties
const agent = new AgentDefinition({
name: "my-agent", // string
model: "gpt-4o", // string
instructions: "...", // string
tools: [], // Tool[]
// ... full autocomplete available
});

The SDK supports both module systems:

// ESM (recommended)
import { AgentDefinition } from "agentschema";
// CommonJS
const { AgentDefinition } = require("agentschema");

Handle parsing errors gracefully:

import { AgentDefinition } from "agentschema";
try {
const agent = AgentDefinition.fromYaml(invalidYaml);
} catch (error) {
console.error("Failed to parse agent definition:", error.message);
}