HOW-TO · DEV

How to integrate an AI assistant into your API client library to auto-generate usage examples

advanced30 minBy Eruo Fredoline
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

  1. Define a schema for the example manifest. Each entry should include: method, language, code, description, and tags.
  2. Write a script (generate-examples.ts or generate_examples.py) that reads all exported methods from the client library using reflect-metadata (TypeScript) or Python's inspect module.
  3. 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.
  4. Iterate over all exported methods, send each to the AI assistant, and parse the JSON response.
  5. Validate each generated example by checking that the JSON parses cleanly and that the code field contains syntactically valid source.
  6. Write the validated examples to examples/manifest.json in the repository root.
  7. 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_KEY or ANTHROPIC_API_KEY is 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.

Related guides