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.0.tar.gz (5.6 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.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: snake_query_sdk-1.0.0.tar.gz
  • Upload date:
  • Size: 5.6 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.0.tar.gz
Algorithm Hash digest
SHA256 3b82ebc00274ba624016254f50916527e8b578acd7e4ffe9e287252e8747fc21
MD5 2a37118756addbda392504bdbc4f846f
BLAKE2b-256 c200098a9b1399936f61ec0752426c423e1dca5bcd7e2a2a11f37d28927db173

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for snake_query_sdk-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1edcd3bbba93abd0dee01e050fc08659fcf7631ac78cf9e439dece035ced883
MD5 ea643fc5f38c11216baf2d07835fcae4
BLAKE2b-256 b98fa815e8b4372c964b076963063ba37c31f222118623fec892d329647cb87d

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