Metadata-Version: 2.4
Name: bloom-gateway-sdk
Version: 1.0.1
Summary: Typed helpers for Bloom Gateway request controls and response metadata
Author-email: lib-py@bloomfactory.ai
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Bloom Gateway SDK for Python

`bloom-gateway-sdk` is a dependency-free helper for applications that call the
Bloom Gateway's OpenAI-compatible inference API. It builds the Gateway's
`bloom` request extension and parses Bloom metadata from responses; your
application continues to own HTTP transport through the OpenAI SDK, `httpx`,
or another client.

## Install

Requires Python 3.10+.

```bash
pip install bloom-gateway-sdk
```

## Call the Gateway

Create a Gateway virtual key and keep it on the server. Pass it to an
OpenAI-compatible client pointed at the Gateway; do not expose virtual keys in
browser or mobile applications. Use a concrete `provider/model` identifier as
the request model.

```python
from openai import OpenAI
from bloom_sdk import BloomOverrides

client = OpenAI(api_key=api_key, base_url="https://gateway.bloomfactory.ai/v1")
response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}],
    extra_body={"bloom": BloomOverrides(routing_type="smart").to_dict()},
)
```

The OpenAI SDK supplies the bearer authentication from `api_key`. To send the
Bloom-branded virtual-key header and optional attribution headers instead, use
`BloomHeaders` with the transport client you already use.

```python
from bloom_sdk import BloomHeaders

headers = BloomHeaders(api_key=api_key, client_id="support-bot").to_dict()
```

`BloomHeaders` rejects ambiguous attribution pairs: choose `client_id` or
`agent_id`, and `trace_id` or `session_id`.

## Request controls

`BloomOverrides` validates and serializes the stable Gateway controls. Omitted
fields are omitted from the request, allowing the Gateway's saved defaults to
apply.

```python
from bloom_sdk import BloomOverrides

overrides = BloomOverrides(
    prompt_compaction="compact",
    semantic_cache_enabled=True,
    routing_type="smart",
    routing_task="coding",
    routing_preference="quality",
)
body = {"bloom": overrides.to_dict()}
```

Supported values are `compact`, `prune`, `trim`, or `none` for
`prompt_compaction`; `strict` or `smart` for `routing_type`; and `cost`,
`latency`, or `quality` for `routing_preference`. Smart Routing uses the
concrete model supplied to the request as its routing baseline.

## Parse responses

For a non-streaming response from a transport client, use `BloomResponse` to
read Bloom response headers and echoed body metadata. Missing metadata remains
`None`.

```python
from bloom_sdk import BloomResponse

bloom = BloomResponse.from_response(http_response)
print(bloom.call_id)
print(bloom.response_cost_float())
print(bloom.routing_task)
if bloom.compaction and bloom.compaction.enabled:
    print(bloom.compaction.chars_removed)
```

For streaming responses, parse the initial SSE response headers with
`BloomStreamResponse.from_headers()`. When the final chunk is available, use
`BloomStreamResponse.from_stream(headers, final_chunk)` to combine header and
final-chunk metadata.

```python
from bloom_sdk import BloomStreamResponse

bloom = BloomStreamResponse.from_stream(stream_headers, final_chunk)
print(bloom.call_id, bloom.request_id)
```

## Scope

This SDK is for direct Gateway inference calls only. It does not provide HTTP
transport, retries, key or workspace management, dashboard API calls, Bloom
API telemetry lookups, or control-plane routes.
