Skip to main content
The Newscatcher Python SDK includes client-side query validation that mirrors the API’s server-side validation. This feature helps you catch invalid query syntax before making API calls.

Overview

Query validation offers these benefits:
  • Immediate feedback: Catch errors without making API calls.
  • Reduced API usage: Avoid requests for invalid queries.
  • Cost savings: Prevent billable API calls for invalid queries.
  • Essential for LLM workflows: Critical when you process thousands of generated queries.
  • Consistent behavior: Match server-side validation exactly.
  • Better developer experience: Get detailed error messages immediately.

Basic usage

Use the validate_query() method to check query syntax before you make API calls:
Basic query validation
The method returns a tuple:
  • is_valid (bool): Whether the query passes validation
  • error_message (str): Detailed error description if validation fails, or empty string if valid

Automatic validation in SDK methods

Query validation is enabled by default in methods like get_all_articles() and get_all_headlines(). You can control this behavior:
SDK method validation
When validation is enabled and a query fails validation, the method raises a ValueError with the specific error message.

Validation rules

The SDK validates queries using the same rules as the Newscatcher API.

Valid patterns

Single words and terms

Invalid patterns

Forbidden characters

Understand automatic AND insertion

The API automatically inserts AND operators between standalone terms, which can create validation conflicts with mixed operator levels. Problem
Common AND insertion conflicts
Solutions
Fix AND insertion conflicts
Always use double quotes for multi-word terms when combining with OR operators to prevent automatic AND insertion conflicts.

Validate multiple queries

For applications that process multiple queries (like LLM-generated queries), you can validate them in bulk: This approach works well when you work with:
  • LLM-generated queries that may have syntax issues.
  • User input that needs validation before processing.
  • Batch processing scenarios where you want to filter valid queries first.
Bulk validation is especially valuable for production applications processing thousands of queries, as it prevents costly API calls for invalid queries.

Best practices

  1. Use exact phrases for multi-word terms: When you search for specific phrases, always use double quotes to prevent automatic AND insertion conflicts.
  2. Validate LLM-generated queries: Essential for applications that process thousands of AI-generated queries to save time and money.
  3. Group complex queries: Use parentheses to make query logic clear and avoid operator-level conflicts.

See also