HOW-TO · DEV
How to integrate an AI assistant into your API client library to auto-generate usage examples
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
Existing API client library in TypeScript/JavaScript or Python, AI API access (OpenAI or Anthropic), Node.js 18+ or Python 3.10+
What this does
Developers publishing API client libraries spend significant time writing and maintaining usage examples. This guide explains how to embed an AI assistant call into the library's build or documentation pipeline so that usage examples are generated automatically from method signatures, docstrings, and type definitions. The generated examples are written to a JSON manifest that documentation generators such as Typedoc, Docusaurus, or Sphinx can ingest and render alongside reference documentation.
Steps
- Define a schema for the example manifest. Each entry should include:
method,language,code,description, andtags. - Write a script (
generate-examples.tsorgenerate_examples.py) that reads all exported methods from the client library usingreflect-metadata(TypeScript) or Python'sinspectmodule. - Construct an AI prompt that includes the method name, signature, and docstring, and instructs the model to produce one idiomatic usage example in each supported language.
- Iterate over all exported methods, send each to the AI assistant, and parse the JSON response.
- Validate each generated example by checking that the JSON parses cleanly and that the
codefield contains syntactically valid source. - Write the validated examples to
examples/manifest.jsonin the repository root. - Integrate the script into the CI pipeline so that examples are regenerated on every version bump or pull request.
Verification
# Verify the examples manifest was written and contains entries
test -f examples/manifest.json && python3 -c "
import json
with open('examples/manifest.json') as f:
data = json.load(f)
assert isinstance(data, list), 'Manifest must be a list'
assert len(data) > 0, 'Manifest is empty'
print(f'OK: {len(data)} examples generated')
"
# Expected: OK: <N> examples generated
Common failures
- AI API key is missing or expired. The script exits with a 401 status. Ensure
OPENAI_API_KEYorANTHROPIC_API_KEYis set in the environment before running. - AI returns Markdown instead of raw JSON. Wrap the AI call with a post-processor that extracts the first JSON code block if the raw response contains backtick fences.
- Examples contain hallucinations (non-existent methods or properties). Add a validation step that validates generated code against the actual TypeScript/Python type definitions before writing to the manifest.
- Rate limiting from the AI API causes incomplete generation. Implement exponential backoff with jitter in the API call loop and persist partial results to allow resume.
- Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
- Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.