Python
- Generate from text:
generate(description, provider=..., model=..., api_key=..., output=...)requires an explicit provider and model, network access, and a key passed withapi_key=or set in that provider's environment variable. - Work from JSON:
from_netlist(netlist, output_format=...)validates and savescircuit.svgin the current directory without an API key. Passoutput=Noneto keep the result in memory. - Check or draw:
validate(netlist)reports structural errors and available electrical checks; it does not simulate the circuit.render(netlist, output_format=...)returns SVG text by default and can return PNG or PDF bytes. - Defaults:
generate()saves SVG in the current directory and uses a 120-second request timeout. Setoutput,output_format, andtimeoutto change them.
The offline examples below use this small LED circuit. Download it as led.json:
Load the file once at the start of your script:
import json
from pathlib import Path
netlist = json.loads(Path("led.json").read_text(encoding="utf-8"))
Check a netlist with validate()
Call validate() when you want to find missing components, invalid pins, or other connection errors before drawing anything. It takes a netlist dictionary and does not create a file.
from circuitgen import validate
check = validate(netlist)
if check.valid:
print("Netlist is valid")
else:
print(check.errors)
The returned ValidationResult includes:
valid: whether structural validation passed.errors: structural problems found, if any.electrical_report: the electrical checks that ran.
Validation does not simulate the circuit or guarantee electrical safety.
Generate schematics from netlists with from_netlist()
Use from_netlist() when you already have a netlist and want a saved schematic plus access to its connections and SVG. It validates and renders the netlist without an API key or internet access.
from circuitgen import from_netlist
result = from_netlist(netlist, output="led.svg")
print(result.output_path)
By default, from_netlist() saves circuit.svg in the current working directory. You can change the output with:
output="path/to/file.svg"to choose a filename or directory.- The filename extension (
.svg,.png, or.pdf) to choose the format. output_format="png"oroutput_format="pdf"to choose the format explicitly.
The input dictionary is left unchanged.
Generate schematics from descriptions with generate()
Use generate() when you want to describe a circuit in words. The provider creates a netlist; CircuitGen checks it, renders it, and saves the schematic. This needs network access and an API key for your chosen provider.
Set up your key
Choose one of these options to provide your API key. Use the environment variable named for your provider (see AI Providers), or pass the key to generate() with api_key=. CircuitGen does not read keys saved in CLI Settings. Avoid putting real keys directly in source code.
Option 1: Set an environment variable in your shell
Set the provider's environment variable before starting Python:
# macOS or Linux
export YOUR_PROVIDER_API_KEY="YOUR_API_KEY"
# PowerShell
$env:YOUR_PROVIDER_API_KEY = "YOUR_API_KEY"
Option 2: Load a .env file
Use a package such as python-dotenv to load the file. CircuitGen does not read .env files itself. Install python-dotenv, then load the file before calling generate():
python -m pip install python-dotenv
YOUR_PROVIDER_API_KEY=your-key-here
import os
from dotenv import load_dotenv
from circuitgen import generate
load_dotenv()
result = generate(
"A battery powering an LED through a resistor.",
provider="YOUR_PROVIDER",
model="YOUR_MODEL_ID",
api_key=os.environ["YOUR_PROVIDER_API_KEY"],
)
Option 3: Pass the key to generate()
Use this when your application gets the key from a secret manager or another secure source:
result = generate(
"A battery powering an LED through a resistor.",
provider="YOUR_PROVIDER",
model="YOUR_MODEL_ID",
api_key=api_key,
)
Choose a provider and model
Pass both provider and model when you want a specific combination. The model ID must be available to your key:
from circuitgen import generate
result = generate(
"A 9 V battery connected in series with a 360 Ω resistor and a red LED.",
provider="YOUR_PROVIDER", # Replace with your provider.
model="YOUR_MODEL_ID", # Replace with a model available to your key.
api_key="YOUR_API_KEY", # Replace with your provider's API key.
output="led.svg"
)
print(result.output_path)
Replace the provider and model placeholders with values your account can use, and provide that provider's API key using one of the options above. provider and model are required; CircuitGen does not choose either automatically. A model or key saved in CLI Settings is not used by the Python library.
generate() uses these defaults:
- Output:
circuit.svgin the current working directory. Setoutput="path/to/file.svg"to choose another path. - Format: SVG. Choose PNG or PDF with the filename extension or
output_format. - Timeout: 120 seconds. Set
timeout=60to use 60 seconds, for example. - Retries and clarifications: both off. Turn them on with
retry_transient=Trueorclarify=True.
Review generated values and connections before using a design.
Work with CircuitResult
generate() and from_netlist() both return a CircuitResult. You can use it after saving a file or keep it for another export.
| Attribute | What it contains |
|---|---|
svg | The schematic as SVG text, even when you saved a PNG or PDF. |
netlist | The netlist with recognized component values normalized. |
model | The parsed circuit model and its connections. |
output_path | The saved file's path, or None when no file was saved. |
output_format | The selected format: svg, png, or pdf. |
electrical_report | The electrical checks performed on the circuit. |
Use these methods to work with the result:
save(path): save the schematic to a file. The extension selects SVG, PNG, or PDF.encode(format): return the content directly, as SVG text or PNG/PDF bytes.
result.save("led.pdf")
pdf_bytes = result.encode("pdf")
To avoid writing the initial file, pass output=None to generate() or from_netlist(). You can save the returned result later. If from_netlist() cannot render an input, it raises NetlistRenderError; its netlist attribute keeps the supplied input.
Render schematics with render()
Use render() when you only need the finished image content, without a file or a CircuitResult. It accepts a netlist dictionary or a validated CircuitModel.
from circuitgen import render
svg_text = render(netlist) # SVG text by default
png_bytes = render(netlist, output_format="png") # Image bytes
render() also supports PDF bytes. Optional parameters include:
output_format: choosesvg,png, orpdf; defaults to SVG.style: pass aSchematicStyleto customize appearance.optimize=True: enable optional layout optimization.
See Customization for styling and the API Reference for the full parameter list.