Skip to content

Add Schema Validation Layer Before Executing LLM-Generated GraphQL Queries #14

Description

@HESleagacy

The current query generation pipeline executes GraphQL queries generated by the LLM without validating the filter structure against the project schema.
Because the filter is produced dynamically by an LLM, it may contain invalid field names, unsupported operators, or incorrect nested paths. These issues are currently only detected when the query is sent to the Guppy API, resulting in runtime errors.
Adding a schema validation step before execution would make the system more robust and allow invalid queries to be caught earlier in the pipeline.


Current

From tracing the code path across the backend utilities, the current flow appears to be:
User Query

Keyword extraction

Schema term mapping (pcdc-schema-prod.json, gitops.json)

LLM generates nested GraphQL filter

convert_to_executable_nested_graphql()

execute_graphql_query()

At the moment, the generated filter is parsed as JSON but not validated against the schema before being executed.
Relevant areas of the code include:

  • nested_graphql_helper.py
  • query_builder.py
  • /nested_graphql endpoint in app.py

Problem

I traced the query generation pipeline across prompt_builder.py, query_builder.py, and nested_graphql_helper.py and could not find a validation step ensuring that the generated filter fields and nested paths conform to the schema before execution.
The system currently relies on prompt instructions to guide the LLM to use correct schema fields, but there is no programmatic validation ensuring that the generated GraphQL filter structure actually conforms to the schema before execution.

Invalid field names

Example:

{
  "AND": [
    {
      "IN": {
        "tumor_stage": ["Metastatic"]
      }
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions