The official Python SDK for SnakeQuery - Transform natural language into structured data queries with AI.
pip install snake-query-sdk
from snake_query_sdk import SnakeQuery, SchemaBuilder
import os
# It's recommended to set your API key as an environment variable
client = SnakeQuery(api_key=os.environ.get('SNAKE_QUERY_API_KEY'))
# Define the expected response structure
product_schema = SchemaBuilder.create() \
.array(
SchemaBuilder.create() \
.object() \
.add_string_property('name') \
.add_number_property('price', minimum=0) \
.required(['name', 'price']) \
.build()
)
.build()
# Make the query
result = client.query(
query='Find products under $100',
fetch_url='https://api.store.com/products',
response_schema=product_schema
)
print(result['response']) # Structured data
- π§ Natural Language Processing: Write queries in plain English
- ποΈ Schema-Driven: Type-safe, validated responses
- π Multiple Data Sources: Query lists, dictionaries, or REST APIs
- β‘ High Performance: Optimized for production use
- π Secure: Built-in authentication and error handling
Transform complex data operations into simple English:
# Instead of complex Python:
# result = sorted(
# [
# {'name': item['title'], 'price': item['price'], 'rating': item['rating']}
# for item in data
# if item['price'] < 500 and item['category'] == 'electronics'
# ],
# key=lambda x: x['rating'],
# reverse=True
# )[:5]
# Use natural language:
result = client.query(
query='Find top 5 electronics under $500, show name, price and rating, sort by rating',
data=products
)
Control output format with schemas:
schema = SchemaBuilder.create() \
.array(
SchemaBuilder.create() \
.object() \
.add_string_property('productName') \
.add_number_property('price', minimum=0) \
.add_number_property('rating', minimum=0, maximum=5) \
.required(['productName', 'price']) \
.build()
)
.build()
result = client.query(
query='Show top rated products',
data=products,
response_schema=schema
)
# Guaranteed structure:
# [{'productName': 'iPhone', 'price': 999, 'rating': 4.8}]
products = [
{ 'name': 'iPhone', 'price': 999, 'category': 'electronics' },
{ 'name': 'Shoes', 'price': 129, 'category': 'fashion' }
]
result = client.query(
query='Find products by category and calculate average price per category',
data=products
)
result = client.query(
query='Show me the 5 most expensive products with their details',
fetch_url='https://api.escuelajs.co/api/v1/products',
response_schema=expensive_products_schema
)
client = SnakeQuery(api_key='your-api-key')
Main method for all query operations:
def query(self, query: str, data: any = None, fetch_url: str = None, response_schema: dict = None, debug: bool = False) -> dict:
# ...
Build type-safe response schemas:
schema = SchemaBuilder.create() \
.array(item_schema) # Array of items
.object() # Object structure
.add_string_property('name') # Add string field
.add_number_property('price') # Add number field
.required(['name']) # Mark fields as required
.build() # Generate schema dictionary
from snake_query_sdk.exceptions import SnakeQueryError
try:
result = client.query(**options)
except SnakeQueryError as e:
if e.status == 401:
print('Invalid API key')
elif e.status == 402:
print('Insufficient credits')
elif e.status == 504:
print('Query timeout - try simplifying')
else:
print(f'Error: {e.message}')
export SNAKE_QUERY_API_KEY="your-api-key-here"
import os
client = SnakeQuery(api_key=os.environ.get('SNAKE_QUERY_API_KEY'))
result = client.query(
query='Analyze data',
data=dataset,
debug=True # Enables detailed logging
)
All successful queries return a dictionary:
{
'usageCount': {
'inputTokens': 150,
'outputTokens': 75,
'totalTokens': 225
},
'response': [
# Your structured data here
]
}
Check out the examples directory for complete working examples.
Run an example:
export SNAKE_QUERY_API_KEY="your-key"
python examples/data_query_demo.py
- Python 3.7+
requests
library- Valid SnakeQuery API key
We welcome contributions! Please see our CONTRIBUTING.md
for details.
MIT License - see LICENSE
file for details.