DocsPromptQL APIPromptQL Execute Program API

PromptQL Program API

(For advanced users)

The PromptQL agent creates PromptQL programs that work on your data. Usually, these programs are created and invoked by interacting within the PromptQL playground.

However, you can also use an API to run these programs dynamically.

API Reference

Execute Program Endpoint

Execute a PromptQL program with your data.

Request

POST https://api.promptql.pro.hasura.io/execute_program

Headers

Content-Type: application/json

Request Body

{
  "code": "<your promptql program code>",
  "promptql_api_key": "<promptql api key created from project settings>",
  "ai_primitives_llm": {
    "provider": "hasura",
  },
  "ddn": {
    "url": "<project sql endpoint url>",
    "headers": {}
  },
  "artifacts": []
}

Request Body Fields

FieldTypeRequiredDescription
codestringYesThe PromptQL program code to execute
promptql_api_keystringYesPromptQL API key created from project settings
ai_primitives_llmobjectYesConfiguration for the AI primitives LLM provider
ddnobjectYesDDN configuration including URL and headers
artifactsarrayYesList of artifacts to provide as context or initial state

LLM Provider Options

The ai_primitives_llm field supports the following providers:

  1. Hasura:
{
  "provider": "hasura"
}
  1. Anthropic:
{
  "provider": "anthropic",
  "api_key": "<your anthropic api key>"
}
  1. OpenAI:
{
  "provider": "openai",
  "api_key": "<your openai api key>"
}

Artifacts

The artifacts array can contain both text and table artifacts:

  1. Text Artifact:
{
  "identifier": "my_text",
  "title": "My Text Document",
  "artifact_type": "text",
  "data": "Text content here"
}
  1. Table Artifact:
{
  "identifier": "my_table",
  "title": "My Data Table",
  "artifact_type": "table",
  "data": [
    {
      "column1": "value1",
      "column2": "value2"
    }
  ]
}

Response

{
  "output": "<program output>",
  "error": null,
  "accessed_artifact_ids": ["artifact1", "artifact2"],
  "modified_artifacts": [
    {
      "identifier": "new_artifact",
      "title": "New Artifact",
      "artifact_type": "table",
      "data": [
        {
          "column1": "value1",
          "column2": "value2"
        }
      ]
    }
  ],
  "llm_usages": [
    {
      "provider": "anthropic",
      "model": "claude-3-5-sonnet-20241022",
      "input_tokens": 691,
      "output_tokens": 33
    }
  ]
}

Response Fields

FieldTypeDescription
outputstringThe program’s output, similar to what you see in the playground
errorstring|nullError message if execution failed, null otherwise
accessed_artifact_idsarrayList of artifact identifiers that were accessed during execution
modified_artifactsarrayList of artifacts that were created or modified during execution
llm_usagesarrayDetails about LLM usage during execution

LLM Usage Fields

FieldTypeDescription
providerstringThe LLM provider used (e.g., “hasura”, “anthropic”, “openai”)
modelstringThe specific model used
input_tokensintegerNumber of input tokens consumed
output_tokensintegerNumber of output tokens generated

Error Response

When the API encounters an error, it will return a 422 status code with a validation error response:

{
  "detail": [
    {
      "loc": ["field_name"],
      "msg": "error message",
      "type": "error_type"
    }
  ]
}

Notes for using the Execute Program API

  1. Program Code

    • Ensure your PromptQL program code is properly formatted and tested
    • You can export working programs from the PromptQL playground using the “Export as API” button
  2. Artifacts

    • Provide all necessary artifacts that your program needs to run
    • Make sure artifact identifiers match those referenced in your program code
    • Both text and table artifacts are supported
  3. Error Handling

    • Always check the error field in the response
    • Implement appropriate retry logic for transient failures
    • Validate your inputs against the API schema to catch issues early

Get PromptQL programs from the playground

You can get PromptQL programs from PromptQL playground interactions by clicking on the “Export as API” button next to the Query Plan in the playground.

Export as API