Skip to main content

A Python SDK for interacting with the Query API service

Project description

Query API SDK

A Python SDK for interacting with Vana's Query API service. This SDK allows you to easily query and transform your AI-generated data, including posts, tweets, and other content generated by your AI models.

Features

  • 🐍 Full Python type hints
  • 🔒 Built-in authentication
  • 🔄 Async-style API
  • 📊 Data transformation support
  • 🔔 Webhook integration
  • 📝 Comprehensive typing
  • ⏱️ Polling utilities for long-running queries

Installation

pip install query-api-sdk
# or
poetry add query-api-sdk

Quick Start

from query_api_sdk import create_client, QueryClientConfig

client = create_client(QueryClientConfig(
    api_key='your-api-key',
    base_url='https://api.vana.ai/query'
))

# Submit a query and get results
def get_my_posts():
    try:
        query_id = client.submit_query({
            "query": "SELECT * FROM reddit_posts WHERE file_owner = 'my_user_id'"
        })
        
        results = client.wait_for_results(query_id)
        print(results)
    except Exception as error:
        print(f"Error: {str(error)}")

Available Data Schemas

The Query API provides access to various Vana-generated content types:

Reddit Posts

reddit_posts {
  file_owner: string     -- User ID of the post owner
  post_id: integer       -- Unique identifier for the post
  title: string         -- Post title
  content: string       -- Post content
}

Twitter Tweets

twitter_tweets {
  file_owner: string    -- User ID of the tweet owner
  tweet_id: integer     -- Unique identifier for the tweet
  text: string         -- Tweet content
}

Detailed Usage

Configuration

from query_api_sdk import create_client, QueryClientConfig

client = create_client(QueryClientConfig(
    api_key='your-api-key',
    base_url='https://api.vana.ai/query',
    timeout=30000  # Optional: default is 10000ms
))

Getting Available Schemas

schemas = client.get_schemas()
print(schemas)

Submitting Queries

Basic query:

query_id = client.submit_query({
    "query": "SELECT * FROM reddit_posts LIMIT 10"
})

With data transformation:

query_id = client.submit_query({
    "query": "SELECT * FROM reddit_posts",
    "transform": """
    def transform(rows):
        return [{**row, "word_count": len(row["content"].split())} for row in rows]
    """
})

With webhook notification:

query_id = client.submit_query({
    "query": "SELECT * FROM twitter_tweets",
    "webhook_url": "https://your-server.com/webhook"
})

Checking Query Status

status = client.get_query_status(query_id)
print(status["status"])  # 'queued' | 'processing' | 'completed' | 'failed'

Getting Results

With pagination:

results = client.get_query_results(
    query_id,
    limit=100,
    cursor="200"
)

Waiting for completion:

results = client.wait_for_results(
    query_id,
    timeout=300000,      # Optional: max time to wait (default: 5 minutes)
    poll_interval=1000   # Optional: time between status checks (default: 1 second)
)

Common Query Examples

Getting Recent Posts

query_id = client.submit_query({
    "query": """
        SELECT *
        FROM reddit_posts
        WHERE file_owner = 'your_user_id'
        ORDER BY post_id DESC
        LIMIT 10
    """
})

Analyzing Content Length

query_id = client.submit_query({
    "query": """
        SELECT *
        FROM reddit_posts
        WHERE file_owner = 'your_user_id'
    """,
    "transform": """
    def transform(rows):
        return [{
            **row,
            "content_length": len(row["content"]),
            "word_count": len(row["content"].split())
        } for row in rows]
    """
})

Combining Data Sources

query_id = client.submit_query({
    "query": """
        SELECT 
            'reddit' as source,
            title as content,
            post_id as id
        FROM reddit_posts
        WHERE file_owner = 'your_user_id'
        UNION ALL
        SELECT 
            'twitter' as source,
            text as content,
            tweet_id as id
        FROM twitter_tweets
        WHERE file_owner = 'your_user_id'
    """
})

Error Handling

The SDK uses a custom QueryAPIError class for error handling:

from query_api_sdk import QueryAPIError

try:
    results = client.get_query_results("invalid-id")
except QueryAPIError as error:
    print(f"API Error: {str(error)}")
    print(f"Status Code: {error.status_code}")
    print(f"Response: {error.response}")

Webhook Integration

When providing a webhook URL, your endpoint will receive POST requests with the following format:

{
    "query_id": str,
    "status": str,  # 'completed' | 'failed'
    "error": Optional[str]
}

Example webhook handler (Flask):

from flask import Flask, request

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    query_id = data["query_id"]
    status = data["status"]
    error = data.get("error")
    
    if status == "completed":
        # Handle completion
        pass
    elif status == "failed":
        # Handle failure
        pass
    
    return "", 200

Rate Limiting

The Query API implements rate limiting to ensure fair usage. The SDK will automatically handle rate limit responses by raising a QueryAPIError with the appropriate status code and message.

Type Hints Support

The SDK is written with full Python type hints and provides comprehensive type definitions for all features. You can import types directly:

from query_api_sdk import (
    QueryStatusType,
    Schema,
    QueryRequest,
    QueryResults
)

Development

For development, clone the repository and install dependencies:

git clone https://github.com/vana-com/query-sdk-python.git
cd query-sdk-python
pip install -e ".[dev]"

Run tests:

pytest

License

MIT License - see LICENSE 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

opendatalabs_query_sdk-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

opendatalabs_query_sdk-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file opendatalabs_query_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: opendatalabs_query_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for opendatalabs_query_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 13be34a1883b7f46b0e4c73b794e8d7bec012bb8151d2f60557cdc27ca427a20
MD5 d574cda48ca3ccf89d629f62666680cf
BLAKE2b-256 3777aa711f32096540148a0eb9df48961e3224efb4623939169820dd6b1f3f1b

See more details on using hashes here.

File details

Details for the file opendatalabs_query_sdk-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for opendatalabs_query_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 818baf0ae5cc6ad2eb96400949b6ec997d12ad1fc65d7d76b0005cf97e37a312
MD5 d4e1571cdc39c2eef33d6f6e177ada3a
BLAKE2b-256 9d63b1daa4a7b671d48a5bae81f5e9e069fd42d6c3d85165b44905ddc54c69d1

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