Using the Emitter
The agentschema-emitter package generates runtime libraries for multiple languages from the AgentSchema TypeSpec definitions. You can use it via CLI or programmatically.
Installation
Section titled “Installation”npm install agentschema-emitterOr use it directly with npx:
npx agentschema-emitter --helpCLI Usage
Section titled “CLI Usage”The emitter provides a CLI command agentschema-generate for generating runtime code:
# Generate all default targets (Python, C#, TypeScript, Go)npx agentschema-generate -o ./generated
# Generate specific targets onlynpx agentschema-generate -o ./lib -t python,csharp
# Generate with a different root objectnpx agentschema-generate -o ./lib -r AgentSchema.AgentDefinition
# Exclude specific models from generationnpx agentschema-generate -o ./lib --omit AgentManifest,ContainerAgentCLI Options
Section titled “CLI Options”| Option | Short | Description | Default |
|---|---|---|---|
--output <dir> | -o | Output directory | Required |
--targets <list> | -t | Comma-separated targets | python,csharp,typescript,go |
--root-object <name> | -r | Root model to generate | AgentSchema.AgentManifest |
--omit <list> | Models to exclude | None | |
--namespace <name> | -n | Override namespace | From TypeSpec |
--no-tests | Skip test generation | false | |
--no-format | Skip code formatters | false |
Programmatic API
Section titled “Programmatic API”You can also use the emitter programmatically in your build scripts:
import { generate } from "agentschema-emitter/generate";
const result = await generate({ output: "./generated", targets: ["python", "csharp", "typescript", "go"], rootObject: "AgentSchema.AgentManifest", omit: ["AgentManifest"], noTests: false, noFormat: false,});
console.log(`Generated ${result.filesGenerated} files`);API Options
Section titled “API Options”interface GenerateOptions { /** Output directory for generated files */ output: string;
/** Target languages to generate */ targets?: ("python" | "csharp" | "typescript" | "go" | "markdown")[];
/** Root TypeSpec model (fully qualified name) */ rootObject?: string;
/** Models to exclude from generation */ omit?: string[];
/** Override the namespace */ namespace?: string;
/** Skip generating tests */ noTests?: boolean;
/** Skip running code formatters */ noFormat?: boolean;}Supported Targets
Section titled “Supported Targets”| Target | Language | Output | Features |
|---|---|---|---|
python | Python 3.10+ | Dataclasses | YAML/JSON serialization, type hints |
csharp | C# 12 | Classes | System.Text.Json, nullable annotations |
typescript | TypeScript | Interfaces | js-yaml, camelCase properties |
go | Go 1.22+ | Structs | encoding/json, gopkg.in/yaml.v3 |
markdown | Markdown | Docs | Reference documentation |
TypeSpec Integration
Section titled “TypeSpec Integration”You can also use the emitter as a TypeSpec emitter in your tspconfig.yaml:
emit: - "agentschema-emitter"
options: "agentschema-emitter": root-object: "AgentSchema.AgentManifest" emit-targets: - type: Python output-dir: "./python" - type: CSharp output-dir: "./csharp" - type: TypeScript output-dir: "./typescript" - type: Go output-dir: "./go"Then run:
npx tsp compile .Generated Code Features
Section titled “Generated Code Features”All generated runtimes include:
- YAML/JSON Loading: Load agent definitions from files
- YAML/JSON Saving: Save agent definitions to files
- Serialization: Convert to/from JSON and YAML strings
- Validation: Type-safe models with proper typing
- Round-trip: Load, modify, and save without data loss
Example: Loading an Agent Definition
Section titled “Example: Loading an Agent Definition”from agentschema import AgentDefinition
# Load from fileagent = AgentDefinition.load("agent.yaml")
# Access propertiesprint(f"Agent: {agent.name}")print(f"Model: {agent.model}")
# Modify and saveagent.description = "Updated description"agent.save("agent-modified.yaml")using AgentSchema;
// Load from filevar agent = AgentDefinition.FromYamlFile("agent.yaml");
// Access propertiesConsole.WriteLine($"Agent: {agent.Name}");Console.WriteLine($"Model: {agent.Model}");
// Modify and saveagent.Description = "Updated description";agent.ToYamlFile("agent-modified.yaml");import { AgentDefinition } from 'agentschema';
// Load from fileconst agent = AgentDefinition.fromYamlFile('agent.yaml');
// Access propertiesconsole.log(`Agent: ${agent.name}`);console.log(`Model: ${agent.model}`);
// Modify and saveagent.description = 'Updated description';agent.toYamlFile('agent-modified.yaml');import "github.com/microsoft/AgentSchema/runtime/go/agentschema"
// Load from fileagent, err := agentschema.LoadAgentDefinition("agent.yaml")if err != nil { log.Fatal(err)}
// Access propertiesfmt.Printf("Agent: %s\n", agent.Name)fmt.Printf("Model: %s\n", agent.Model)
// Modify and saveagent.Description = "Updated description"agent.Save("agent-modified.yaml")Customizing Generation
Section titled “Customizing Generation”Excluding Models
Section titled “Excluding Models”If you only need a subset of models, use --omit:
# Generate only AgentDefinition (exclude manifest and container types)npx agentschema-generate -o ./lib --omit AgentManifest,ContainerAgentCustom Root Object
Section titled “Custom Root Object”By default, the emitter generates the full type graph starting from AgentManifest. To start from a different type:
# Generate from AgentDefinition as rootnpx agentschema-generate -o ./lib -r AgentSchema.AgentDefinitionSingle Target
Section titled “Single Target”For projects using only one language:
# Python onlynpx agentschema-generate -o ./python -t python
# C# onlynpx agentschema-generate -o ./csharp -t csharpNext Steps
Section titled “Next Steps”- Python Runtime - Python-specific features
- C# Runtime - C#-specific features
- TypeScript Runtime - TypeScript-specific features
- Go Runtime - Go-specific features