Opik Query Language (OQL)

OQL provides a powerful, SQL-like syntax for filtering data in Opik. It’s used with various SDK methods like searchPrompts() to find exactly the data you need using expressive filter conditions.

Operators

String Operators

1// Exact match
2'name = "greeting-prompt"';
3
4// Not equal
5'name != "old-prompt"';
6
7// Contains substring (case-insensitive)
8'name contains "greeting"';
9
10// Does not contain substring
11'name not_contains "deprecated"';
12
13// Starts with prefix
14'name starts_with "prod-"';
15
16// Ends with suffix
17'name ends_with "-v2"';

Comparison Operators

1// Greater than (alphabetical for strings)
2'name > "m"'; // Names starting with n-z
3
4// Less than (alphabetical for strings)
5'name < "m"'; // Names starting with a-l

List Operators

1// Check if list contains value
2'tags contains "production"';
3
4// Check if list does not contain value
5'tags not_contains "experimental"';

Combining Conditions

Use AND to combine multiple filter conditions. All conditions must be true for a result to match:

1// Multiple conditions with AND
2'tags contains "production" AND name contains "greeting" AND created_by = "user@example.com"';
3
4// Complex multi-field filtering
5'name starts_with "prod-" AND tags contains "stable" AND name not_contains "deprecated"';

Currently, only AND logic is supported. OR logic is not available in OQL.

Examples

1import { Opik } from "opik";
2
3const client = new Opik();
4
5// Filter by single tag
6const prod = await client.searchPrompts('tags contains "production"');
7
8// Combine name pattern and tag
9const approved = await client.searchPrompts(
10 'name starts_with "prod-" AND tags contains "qa-approved"'
11);
12
13// Multiple conditions
14const results = await client.searchPrompts(
15 'created_by = "user@example.com" AND tags contains "production" AND name not_contains "deprecated"'
16);

These examples use searchPrompts() but the same OQL syntax works with other search methods. Specific resource types may support additional fields - see their respective documentation.

Syntax Rules

String Values

Always wrap string values in double quotes:

1// βœ… Correct - double quotes around values
2await client.searchPrompts('name = "my-prompt"');
3await client.searchPrompts('tags contains "production"');
4
5// ❌ Incorrect - missing quotes
6await client.searchPrompts("name = my-prompt"); // Will fail
7
8// ❌ Incorrect - single quotes
9await client.searchPrompts("name = 'my-prompt'"); // Will fail

Error Handling

1try {
2 const results = await client.searchPrompts("invalid syntax ===");
3} catch (error) {
4 console.error("Invalid OQL syntax:", error.message);
5}

Best Practices

  1. Use descriptive tag hierarchies - Structure tags like "production", "staging", "team-alpha" for effective filtering
  2. Use naming conventions - Implement consistent naming patterns (e.g., "prod-" prefix) to enable powerful filtering
  3. Handle errors - Always wrap OQL queries in try-catch blocks to handle syntax errors gracefully