# Extract structured JSON from messy text with Telnyx AI Inference

> Source: <https://github.com/team-telnyx/telnyx-code-examples/tree/main/extract-structured-json-with-ai-python>
> Published: 2026-06-25 22:25:51+00:00

| name | extract-structured-json-with-ai | |
|---|---|---|
| title | Extract Structured JSON with AI | |
| description | Extract structured JSON from support tickets, emails, leads, or incident reports with Telnyx AI Inference. | |
| language | python | |
| framework | flask | |
| telnyx_products |
|

Extract structured JSON from support tickets, emails, leads, or incident reports with Telnyx AI Inference.

**AI Inference**:`POST /v2/ai/chat/completions`

-[API reference](https://developers.telnyx.com/api/inference/chat-completions)

```
  Unstructured text
        |
        v
  Flask API validates request
        |
        v
  Telnyx AI Inference extracts JSON
        |
        v
  Structured JSON response
```

Copy `.env.example`

to `.env`

and fill in:

| Variable | Type | Example | Required | Description | Where to get it |
|---|---|---|---|---|---|
`TELNYX_API_KEY` |
`string` |
`KEY0123456789ABCDEF` |
yes |
Telnyx API v2 key |
|

`AI_MODEL`

`string`

`moonshotai/Kimi-K2.6`

[Models](https://developers.telnyx.com/docs/inference/models)`HOST`

`string`

`127.0.0.1`

`PORT`

`integer`

`5000`

```
git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/extract-structured-json-with-ai-python
cp .env.example .env
pip install -r requirements.txt
python app.py
```

Extract structured data from text. If you do not provide a schema, the app uses a default support-ticket schema.

```
curl -X POST http://localhost:5000/extract \
  -H "Content-Type: application/json" \
  -d '{
    "text": "Account: Acme Health. Production verification jobs started failing after an API key rotation. Users cannot finish signup. Logs show 401 errors."
  }'
```

**Response:**

```
{
  "model": "moonshotai/Kimi-K2.6",
  "result": {
    "company": "Acme Health",
    "category": "authentication",
    "priority": "urgent",
    "summary": "Production verification jobs are failing after an API key rotation.",
    "affected_environment": "production",
    "affected_region": "unknown",
    "customer_impact": "Users cannot finish signup.",
    "error_codes": ["401"],
    "suspected_cause": "The new API key may be invalid or missing required permissions.",
    "requested_action": "Check API key configuration and permissions."
  }
}
```

Returns sample text and the default schema.

Returns service status and configured model.

| Issue | Cause | Fix |
|---|---|---|
`401 Unauthorized` |
Invalid or missing Telnyx API key | Verify `TELNYX_API_KEY` in `.env` |
`400 Bad Request` |
Missing `text` or invalid `schema` |
Send a non-empty `text` string and a JSON object schema |
`502 Model response was not valid JSON` |
The selected model did not return parseable JSON | Retry with the default model or simplify the schema |

[Run LLM Inference (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/run-llm-inference-python/README.md)[Build RAG with Telnyx Inference (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/build-rag-with-telnyx-inference-python/README.md)[Fax to Structured Data Pipeline (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/fax-to-structured-data-pipeline-python/README.md)

Telnyx is an **AI Communications Infrastructure** platform - voice, messaging, SIP, AI, and IoT on one private, global network.
