Converting Natural Language to Structured GraphQL Queries Using LLMs

Asaf Fried, Daniel Pienica, Sergey Volkovich22 Jan 2025

Problem

Cato Networks sells a cloud network and security service. Its management console has an Events page that can hold millions of events per account. Users narrow them down with a filter bar, where each filter is a key, an operator and a value, plus a time range. Building these filters by hand takes time and requires knowing the product glossary, which new users do not know.

Example from the article: the query "Security events with high risk level from IPS and Anti Malware engines" must become three filters: risk level is High, event type is Security, event subtype is in IPS and Anti Malware.

Solution in brief

The model returns JSON that follows a JSON schema given in the system prompt, and the service validates that JSON against the same schema. Plain code turns a valid result into the GraphQL request. Prompt engineering was enough, so no RAG or fine-tuning was used.

Questions this case answers
  • How to convert free-text user requests into validated structured API filters when the model output must never break the downstream API
  • How to choose an LLM for a structured-output feature using a labeled benchmark and a hard error-rate gate
  • How to let users filter a large event table with free text instead of product-specific filter syntax?
  • How to make sure an LLM never sends an invalid filter to a production API?

Tools and methods

  • Prompt engineering — system prompt with the task, the full schema and the current date and weekday
  • JSON schema validation — checks keys, operators and values of every response and classifies failures
  • Amazon Bedrock — serves the models and makes switching between them cheap
  • Claude 3.5 Sonnet — the model chosen after the benchmark (anthropic.claude-3-5-sonnet-20241022-v2:0)
Continue reading — it's free

Google or a one-time email code. No password.