Python Runtime
Full API reference with async support and type hints.
AgentSchema provides official runtime libraries for multiple programming languages, making it easy to load, create, and manipulate agent definitions programmatically.
pip install agentschemaOr using uv:
uv add agentschemanpm install agentschemaOr using other package managers:
yarn add agentschemapnpm add agentschemadotnet add package AgentSchemaOr via NuGet Package Manager:
Install-Package AgentSchemago get github.com/microsoft/agentschema-go/agentschemaParse a YAML string into an agent definition:
from agentschema import AgentDefinition
yaml_content = """name: my-agentmodel: gpt-4oinstructions: You are a helpful assistant."""
agent = AgentDefinition.from_yaml(yaml_content)print(agent.name) # "my-agent"import { AgentDefinition } from 'agentschema';
const yamlContent = `name: my-agentmodel: gpt-4oinstructions: You are a helpful assistant.`;
const agent = AgentDefinition.fromYaml(yamlContent);console.log(agent.name); // "my-agent"using AgentSchema;
var yamlContent = @"name: my-agentmodel: gpt-4oinstructions: You are a helpful assistant.";
var agent = AgentDefinition.FromYaml(yamlContent);Console.WriteLine(agent.Name); // "my-agent"package main
import ( "fmt" "github.com/microsoft/agentschema-go/agentschema")
func main() { yamlContent := `name: my-agentkind: promptmodel: gpt-4oinstructions: You are a helpful assistant.` agent, _ := agentschema.PromptAgentFromYAML(yamlContent) fmt.Println(agent.Name) // "my-agent"}Load agent definitions directly from files:
from agentschema import AgentDefinition
# Load from YAML fileagent = AgentDefinition.from_yaml_file("agent.yaml")
# Load from JSON fileagent = AgentDefinition.from_json_file("agent.json")import { AgentDefinition } from 'agentschema';import { readFileSync } from 'fs';
// Load from YAML fileconst yaml = readFileSync('agent.yaml', 'utf-8');const agent = AgentDefinition.fromYaml(yaml);
// Load from JSON fileconst json = readFileSync('agent.json', 'utf-8');const agentFromJson = AgentDefinition.fromJson(json);using AgentSchema;
// Load from YAML filevar yaml = File.ReadAllText("agent.yaml");var agent = AgentDefinition.FromYaml(yaml);
// Load from JSON filevar json = File.ReadAllText("agent.json");var agentFromJson = AgentDefinition.FromJson(json);package main
import ( "os" "github.com/microsoft/agentschema-go/agentschema")
func main() { // Load from YAML file yamlBytes, _ := os.ReadFile("agent.yaml") agent, _ := agentschema.PromptAgentFromYAML(string(yamlBytes))
// Load from JSON file jsonBytes, _ := os.ReadFile("agent.json") agentFromJson, _ := agentschema.PromptAgentFromJSON(string(jsonBytes))}Create agent definitions using strongly-typed classes:
from agentschema import AgentDefinition, FunctionTool, Property
agent = AgentDefinition( name="weather-agent", description="Helps users check the weather", model="gpt-4o", instructions="You help users check the weather.", tools={ "get_weather": FunctionTool( name="get_weather", description="Get current weather for a location", parameters={ "location": Property( kind="string", description="City name", required=True, ), }, ), },)import { AgentDefinition, FunctionTool, Property } from 'agentschema';
const agent = new AgentDefinition({ name: 'weather-agent', description: 'Helps users check the weather', model: 'gpt-4o', instructions: 'You help users check the weather.', tools: { get_weather: new FunctionTool({ name: 'get_weather', description: 'Get current weather for a location', parameters: { location: new Property({ kind: 'string', description: 'City name', required: true, }), }, }), },});using AgentSchema;
var agent = new AgentDefinition{ Name = "weather-agent", Description = "Helps users check the weather", Model = "gpt-4o", Instructions = "You help users check the weather.", Tools = new Dictionary<string, Tool> { ["get_weather"] = new FunctionTool { Name = "get_weather", Description = "Get current weather for a location", Parameters = new Dictionary<string, Property> { ["location"] = new Property { Kind = "string", Description = "City name", Required = true, }, }, }, },};package main
import "github.com/microsoft/agentschema-go/agentschema"
func strPtr(s string) *string { return &s }func boolPtr(b bool) *bool { return &b }
func main() { agent := agentschema.PromptAgent{ Kind: "prompt", Name: "weather-agent", Description: strPtr("Helps users check the weather"), Model: agentschema.Model{Name: "gpt-4o"}, Instructions: strPtr("You help users check the weather."), Tools: []agentschema.Tool{ agentschema.FunctionTool{ Kind: "function", Name: "get_weather", Description: strPtr("Get current weather for a location"), Parameters: map[string]agentschema.Property{ "location": { Kind: "string", Description: strPtr("City name"), Required: boolPtr(true), }, }, }, }, }}Export agent definitions to YAML or JSON:
# Convert to YAML stringyaml_str = agent.to_yaml()
# Convert to JSON stringjson_str = agent.to_json(indent=2)
# Save to filesagent.to_yaml_file("output.yaml")agent.to_json_file("output.json")
# Convert to dictionarydata = agent.save()import { writeFileSync } from 'fs';
// Convert to YAML stringconst yamlStr = agent.toYaml();
// Convert to JSON stringconst jsonStr = agent.toJson();
// Save to fileswriteFileSync('output.yaml', agent.toYaml());writeFileSync('output.json', agent.toJson());
// Convert to objectconst data = agent.save();// Convert to YAML stringvar yamlStr = agent.ToYaml();
// Convert to JSON stringvar jsonStr = agent.ToJson();
// Save to filesFile.WriteAllText("output.yaml", agent.ToYaml());File.WriteAllText("output.json", agent.ToJson());
// Convert to dictionaryvar data = agent.Save();import "os"
// Convert to YAML stringyamlStr, _ := agent.ToYAML()
// Convert to JSON stringjsonStr, _ := agent.ToJSON()
// Save to filesos.WriteFile("output.yaml", []byte(yamlStr), 0644)os.WriteFile("output.json", []byte(jsonStr), 0644)
// Convert to mapctx := agentschema.NewSaveContext()data := agent.Save(ctx)Customize loading and saving behavior with context objects:
from agentschema import AgentDefinition, LoadContext, SaveContext
# Custom load processingdef pre_process(data: dict) -> dict: print(f"Loading: {data.get('name')}") return data
load_ctx = LoadContext(pre_process=pre_process)agent = AgentDefinition.from_yaml(yaml_content, load_ctx)
# Custom save formattingsave_ctx = SaveContext( collection_format="object", # Use object format use_shorthand=True, # Use shorthand notation)yaml_str = agent.to_yaml(save_ctx)import { AgentDefinition, LoadContext, SaveContext } from 'agentschema';
// Custom load processingconst loadCtx = new LoadContext({ preProcess: (data) => { console.log(`Loading: ${data.name}`); return data; },});const agent = AgentDefinition.fromYaml(yamlContent, loadCtx);
// Custom save formattingconst saveCtx = new SaveContext({ collectionFormat: 'object', useShorthand: true,});const yamlStr = agent.toYaml(saveCtx);using AgentSchema;
// Custom load processingvar loadCtx = new LoadContext{ PreProcess = (data) => { Console.WriteLine($"Loading: {data["name"]}"); return data; },};var agent = AgentDefinition.FromYaml(yamlContent, loadCtx);
// Custom save formattingvar saveCtx = new SaveContext{ CollectionFormat = CollectionFormat.Object, UseShorthand = true,};var yamlStr = agent.ToYaml(saveCtx);import "github.com/microsoft/agentschema-go/agentschema"
// Custom load processingloadCtx := agentschema.NewLoadContext()loadCtx.PreProcess = func(data map[string]interface{}) map[string]interface{} { fmt.Printf("Loading: %v\n", data["name"]) return data}
// Custom save formattingsaveCtx := agentschema.NewSaveContext()saveCtx.CollectionFormat = agentschema.CollectionFormatObjectsaveCtx.UseShorthand = truedata := agent.Save(saveCtx)Choose the runtime that matches your development environment:
For detailed API documentation, select your language:
Python Runtime
Full API reference with async support and type hints.
TypeScript Runtime
Full API reference with TypeScript types.
C# Runtime
Full API reference with .NET integration.
Go Runtime
Full API reference with type-safe structs.