Skip to main content

Python SDK for Snake Query API - Natural language data querying with AI

Project description

SnakeQuery Python SDK

PyPI version License: MIT

The official Python SDK for SnakeQuery - Transform natural language into structured data queries with AI.

🚀 Quick Start

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

✨ Features

  • 🧠 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

📖 Documentation

🎯 Core Concepts

Natural Language Queries

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
)

Structured Responses

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}]

💻 Usage Examples

Query Direct Data

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
)

Query External APIs

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
)

🔧 API Reference

SnakeQuery Constructor

client = SnakeQuery(api_key='your-api-key')

query()

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:
    # ...

SchemaBuilder

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

⚠️ Error Handling

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}')

🌟 Advanced Features

Environment Variables

export SNAKE_QUERY_API_KEY="your-api-key-here"
import os
client = SnakeQuery(api_key=os.environ.get('SNAKE_QUERY_API_KEY'))

Debug Mode

result = client.query(
    query='Analyze data',
    data=dataset,
    debug=True  # Enables detailed logging
)

📊 Response Format

All successful queries return a dictionary:

{
  'usageCount': {
    'inputTokens': 150,
    'outputTokens': 75,
    'totalTokens': 225
  },
  'response': [
    # Your structured data here
  ]
}

🚀 Examples

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

📝 Requirements

  • Python 3.7+
  • requests library
  • Valid SnakeQuery API key

🤝 Contributing

We welcome contributions! Please see our CONTRIBUTING.md for details.

📄 License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snake_query_sdk-1.0.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snake_query_sdk-1.0.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file snake_query_sdk-1.0.2.tar.gz.

File metadata

  • Download URL: snake_query_sdk-1.0.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for snake_query_sdk-1.0.2.tar.gz
Algorithm Hash digest
SHA256 15b18984e8b0309674bea1b1b643fec28ccac81c2331c3fcb5e53812ff3ca69c
MD5 9740d91606b44ad8697aca34fcbb7c83
BLAKE2b-256 94ee0ec18816da186d95d72e9711f861d70f0093c7bcf694e7b1f3217b820400

See more details on using hashes here.

File details

Details for the file snake_query_sdk-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for snake_query_sdk-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3dc3eb1612b709d7673f5d2a8fbaeeaca9de483f8e1b4dbfe9b4e210b3e915f3
MD5 68d5358260558d06dd6652dc526e3233
BLAKE2b-256 04a2cc292005f99725fd5d9191e8ee814beca72d92a3a0e4cd8379a5874d8798

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page