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.1.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.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: opendatalabs_query_sdk-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 7cb395fe12073351e35eff0e399991b4ab210af4832a97be45f621d4cc3c95b7
MD5 f5b0aa59ac4654d6586a4143cdb788e6
BLAKE2b-256 c450d8e8f9410177a226ed963e39d0590985ab09e9ff85ab2f94a6e1b4092a42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for opendatalabs_query_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 738b0d9ff860e821f16d8f7d06d8582ca4a8c55df8ed1282a6245e0dcac4bdf1
MD5 c61836ccd5927c6a65921d329ddf9790
BLAKE2b-256 3a3c899523ab1bb689accd9598600f2368a8f9d4a33e75bea53ab9b5dd44ed8d

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