Skip to main content

Python SDK for interacting with Somia Agent API

Project description

Somia Python SDK

Alpha release (0.1.0a1) — early SDK for testing and evaluation. APIs may change. Install with pip install somia --pre or pin pip install somia==0.1.0a1.

Official Python client for the Somia Agent API.

The SDK is open source under the MIT license. Access to the Somia platform requires a Somia account and API key; usage of the hosted API is subject to Somia's terms and billing.

Supported endpoints

  • POST /api/v1/external/agent/{agent_id}/session — create a session
  • POST /api/v1/external/agent/{agent_id}/session/{session_id} — continue a session

Installation

From PyPI (pre-release):

pip install somia --pre

Or pin the exact version:

pip install somia==0.1.0a1

From source (development):

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

Requires Python 3.11+.

Quickstart

from somia import SomiaClient

with SomiaClient(
    base_url="https://api.somia-platform.com",
    api_key="your_api_key",
) as client:
    response = client.sessions.create_session(
        agent_id=123,
        input_data="Hello",
        pipeline_version="production",
    )
    print(response.session_id, response.message)

Continue an existing session

from somia import SomiaClient

with SomiaClient(
    base_url="https://api.somia-platform.com",
    api_key="your_api_key",
) as client:
    response = client.sessions.interact_session(
        agent_id=123,
        session_id="550e8400-e29b-41d4-a716-446655440000",
        input_data="Can you explain it in one paragraph?",
    )
    print(response.message)

You can also use client.sessions.run(...) with an optional session_id to create or continue in one call.

Streaming responses (SSE)

from somia import SomiaClient

with SomiaClient(
    base_url="https://api.somia-platform.com",
    api_key="your_api_key",
) as client:
    events = client.sessions.create_session(
        agent_id=123,
        input_data="Stream this answer",
        stream=True,
    )
    for event in events:
        if event.event_type == "chunk":
            print("chunk:", event.data)
        elif event.event_type == "error":
            print("error:", event.data)
        elif event.event_type == "end":
            print("stream ended")

Authentication

By default the SDK sends your API key in the x-api-key header. Override the header name if needed:

client = SomiaClient(
    base_url="https://api.somia-platform.com",
    api_key="your_api_key",
    api_key_header="apikey",
)

Error handling

from somia import AuthError, RateLimitError, ServiceUnavailableError, SomiaClient

try:
    with SomiaClient(base_url="https://api.somia-platform.com", api_key="...") as client:
        client.sessions.create_session(agent_id=123, input_data="Hi")
except AuthError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limit exceeded")
except ServiceUnavailableError as exc:
    print(f"Retry later: {exc}")

Common error classes:

  • BadRequestError — invalid payloads
  • AuthError — missing or invalid API key
  • PermissionDeniedError — access or usage-limit failures
  • NotFoundError — missing agents or sessions
  • RateLimitError — rate limits exceeded
  • ServiceUnavailableError — platform saturation or timeouts
  • ServerError — unexpected server-side failures
  • TransportError — network-level failures
  • StreamParseError — malformed SSE payloads

Session flow notes

  • Create-session may ignore input_data when Begin has no required query fields.
  • Interact-session always uses session_id in the path and forwards input_data as the turn payload.
  • Non-streaming responses include fields such as session_id, message, history, pending, and finished.

Development

See CONTRIBUTING.md for local setup, testing, versioning, and release instructions.

License

MIT — see LICENSE.

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

somia-0.1.0a1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

somia-0.1.0a1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file somia-0.1.0a1.tar.gz.

File metadata

  • Download URL: somia-0.1.0a1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for somia-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 bf9df06caae3b4ee00ad1999b11d3477b489498269ea3c8266742677704663e6
MD5 d5550a354ce188fbe6296ad586f1036b
BLAKE2b-256 ca8c68b983be49d9ff82eaab428c3d9ccbb4e7fc84e98a4aeb0d124fe0b73f96

See more details on using hashes here.

File details

Details for the file somia-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: somia-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for somia-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 45ac9b3cc7128f38c3fee62a14e30adc209772bed3fea4e77d76255b32004310
MD5 7b2146445174fa5ac850d11f94a52369
BLAKE2b-256 eb3a54ea91dd16d1a1fc6e92960f62dbed7036bf971337663e5a790f29a5c633

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