Use Telnyx AI Inference to convert messy text like support tickets and inbound emails into predictable JSON objects your app can route, store, and act on.
Every operations team has a pile of text that is useful but hard to automate: support tickets, inbound emails, incident notes, sales leads, account updates, billing questions. The information is in there, but it is trapped in paragraphs.
The usual next step is a brittle parser. You add a regex for account names, a keyword list for priority, a few string splits for error codes, and suddenly the system works for three examples and breaks on the fourth.
This example shows a better pattern: use Telnyx AI Inference to convert messy text into a predictable JSON object that your app can route, store, and act on.
The full example is a small Flask API:
POST /extract accepts unstructured text and an optional JSON SchemaGET /sample returns sample support text and the default schemaGET /health reports the configured modelThe app calls Telnyx AI Inference through POST /v2/ai/chat/completions and asks the model to return one valid JSON object.
LLMs are great at reading messy language. Applications are great at acting on structured data. Structured extraction is the bridge between the two.
Given a support ticket like this:
The app returns:
That JSON can feed a ticket router, alerting rule, CRM enrichment step, or analytics pipeline. The model handles the language variance; your application handles the workflow.
There are only three moving pieces:
textresponse_format: {"type": "json_object"}Set your API key in .env:
Then call the extraction endpoint:
The extraction function sends the target schema and the user text to Telnyx AI Inference:
The server validates that text is present, checks that schema is a JSON object when provided, parses the model output, and returns a clean response.
The default schema is tuned for support tickets. For sales leads, send your own:
This is the useful part: the same endpoint can power many workflows. Change the schema, not the parser.
Keep schemas small and specific. Large, ambiguous schemas make extraction harder to validate and debug.
Validate model output before writing to a database. The example parses JSON, but production systems should also validate required fields and enum values.
Do not expose the local Flask API publicly without authentication. Treat incoming text as customer data and avoid logging sensitive payloads.
Use deterministic settings for extraction. temperature: 0 is intentional here because the goal is consistency, not creativity.
The example is available in the Telnyx code examples repo:
https://github.com/team-telnyx/telnyx-code-examples/tree/main/extract-structured-json-with-ai-python
It includes the Flask app, quickstart, API reference, guide, requirements file, and environment template.
Related articles