DEV Community

Sonam
Sonam

Posted on

Build an AI Error Explainer in Python

Stack traces are useful, but they are not always easy to act on quickly.

When something breaks, you usually want more than the exception name. You want to know the likely root cause, how serious it is, where to look, and what fix to try first.

This Python example turns a stack trace into structured debugging JSON using Telnyx AI Inference.

Code: https://github.com/team-telnyx/telnyx-code-examples/tree/feat/error-explainer-python/error-explainer-python

What it does

The Flask app exposes:

POST /explain
GET /analyses
GET /analyses/<id>
GET /health
Enter fullscreen mode Exit fullscreen mode

POST /explain accepts a stack trace, plus optional language and runtime context:

{
  "language": "python",
  "context": "Flask production server with gunicorn",
  "stack_trace": "Traceback (most recent call last):\n  File \"app.py\", line 42..."
}
Enter fullscreen mode Exit fullscreen mode

The app sends the trace to Telnyx AI Inference and asks for structured JSON:

{
  "root_cause": "The outbound HTTP call is failing because the downstream service is unreachable.",
  "severity": "high",
  "confidence": 0.91,
  "likely_culprit": "app.py:42",
  "suggested_fix": "Add timeout handling and retry logic around the request.",
  "fix_snippet": "resp = requests.post(url, timeout=15)",
  "related_errors": ["requests.exceptions.Timeout"],
  "prevention": "Set explicit timeouts for outbound HTTP calls."
}
Enter fullscreen mode Exit fullscreen mode

Why this shape is useful

Plain text explanations are nice for humans. Structured output is useful for apps.

With this response shape, you could:

  • show severity in a dashboard
  • send high-severity errors to Slack
  • attach a suggested fix to a CI failure
  • store analyses for recurring incidents
  • build an internal debugging assistant

The example stores recent analyses in memory and lets you retrieve them by ID.

Run it

Clone the examples repo:

git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples
git switch feat/error-explainer-python
cd error-explainer-python
Enter fullscreen mode Exit fullscreen mode

Create your .env file:

cp .env.example .env
Enter fullscreen mode Exit fullscreen mode

Add your Telnyx API key:

TELNYX_API_KEY=your_telnyx_api_key
AI_MODEL=moonshotai/Kimi-K2.6
HOST=127.0.0.1
Enter fullscreen mode Exit fullscreen mode

Install and start:

pip install -r requirements.txt
python app.py
Enter fullscreen mode Exit fullscreen mode

Try the health endpoint:

curl http://localhost:5000/health
Enter fullscreen mode Exit fullscreen mode

Explain an error:

curl -X POST http://localhost:5000/explain \
  -H "Content-Type: application/json" \
  -d '{
    "language": "python",
    "context": "Flask production server",
    "stack_trace": "KeyError: user_id"
  }' | python3 -m json.tool
Enter fullscreen mode Exit fullscreen mode

Where this could go

This is a small example, but it maps pretty cleanly to real developer workflows:

  • CI failure explanation
  • incident triage
  • Slack alerts
  • support tooling
  • internal platform dashboards
  • framework-specific debugging assistants

The useful part is not only that a model can explain an error. It is that the app gets back a predictable object it can route, store, display, or review.

Resources

Telnyx AI skills and toolkits: https://github.com/team-telnyx/ai

Telnyx AI Inference docs: https://developers.telnyx.com/docs/inference

Telnyx Portal: https://portal.telnyx.com/

Top comments (0)