Skip to content

Add query validation for PromQL and LogQL #430

@cbcoutinho

Description

@cbcoutinho

Problem

LLMs often generate invalid PromQL or LogQL queries due to syntax errors. Currently, these errors are only discovered after the query is sent to Prometheus/Loki, which:

  • Wastes API calls and context window space
  • Creates unnecessary iteration cycles
  • Provides unclear error messages
  • Slows down LLM query generation workflows

Proposed Solution

Add query validation using the official parsers from Prometheus and Loki before executing queries.

Implementation

Add validation functions using production-grade parsers:

  • ValidatePromQL(): Uses github.com/prometheus/prometheus/promql/parser
  • ValidateLogQL(): Uses github.com/grafana/loki/v3/pkg/logql/syntax

Integrate inline validation into existing tools:

  • query_prometheus: Validate PromQL before execution
  • query_loki_logs: Validate LogQL before execution

Dependencies

  • Prometheus parser: Already available in github.com/prometheus/prometheus
  • Loki parser: Add github.com/grafana/loki/v3/pkg/logql/syntax

Benefits

  1. For LLMs: Reduced context usage and faster iteration
  2. For users: Immediate feedback on query syntax
  3. For API: Fewer invalid requests
  4. For debugging: Clear, specific error messages

Testing

Comprehensive unit tests covering valid queries, invalid syntax, and edge cases.

Alternative Considered

Creating separate validate_promql and validate_logql tools was considered, but inline validation is simpler and provides immediate feedback without additional tool calls.

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