Metadata-Version: 2.4
Name: bluejay-sdk
Version: 0.5.0
Summary: Bluejay SDK: the Bluejay API client and agent tracing in one package.
Author: Bluejay
License: Apache-2.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: httpcore>=1.0.9
Requires-Dist: httpx>=0.28.1
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.0
Requires-Dist: opentelemetry-sdk>=1.0
Requires-Dist: pydantic>=2.11.2
Requires-Dist: typing_extensions>=4.0.0
Provides-Extra: livekit
Requires-Dist: livekit-agents>=1.5; extra == "livekit"
Provides-Extra: pipecat
Requires-Dist: pipecat-ai>=1.16; python_version >= "3.11" and extra == "pipecat"
Requires-Dist: av>=14.0; python_version >= "3.11" and extra == "pipecat"
Requires-Dist: numpy; python_version >= "3.11" and extra == "pipecat"

# bluejay-sdk

One client for the Bluejay API and for tracing your agent's calls into Bluejay conversations.

```bash
pip install bluejay-sdk               # API client + tracing
pip install "bluejay-sdk[livekit]"    # + LiveKit Agents
pip install "bluejay-sdk[pipecat]"    # + Pipecat
```

```python
from bluejay import BluejayClient

client = BluejayClient()   # reads BLUEJAY_API_KEY; or BluejayClient(api_key="bj_...", base_url=...)
```

In asyncio code, use `AsyncBluejayClient`: the same client, with API methods to `await`.

## Tracing

For API calls, create a client with `BluejayClient()`. For chat, LiveKit, or Pipecat,
create the client once at process startup with tracing enabled:

```python
client = BluejayClient(tracing=True)
```

This adds Bluejay's exporter to your OpenTelemetry setup or creates one.

It also traces model requests through these SDKs. When LiveKit or Pipecat is already
tracing a model request, Bluejay uses the framework's span for that request.

| SDK | Traced |
| --- | --- |
| `openai` | chat completions and responses: `create`, `parse`, `stream`; Azure OpenAI and OpenAI-compatible APIs |
| `anthropic` | messages: `create` and `stream` |
| `google-genai` | Gemini and Vertex AI: `generate_content`, `generate_content_stream`, chats |
| `boto3` | Bedrock `converse` and `converse_stream` |

Each call is one span with the model, prompt, reply, tool calls, tokens and finish reason, in
OpenTelemetry's `gen_ai.*` attributes. Inside `session()` or a voice integration it joins that call.

## Voice agents

LiveKit: one line before the `session.start()` you already have. Spans, audio, transcript and tool
calls all reach Bluejay, and the call becomes a conversation when it hangs up.

```python
client.instrument_livekit(ctx, session, external_agent_id="support-bot")
await session.start(room=ctx.room, agent=SupportAgent())
```

Pipecat: add the Bluejay tap after your output transport and keep your existing context aggregators.
Enable Pipecat's native spans on the worker and use the tap's session ID.

```python
tap = client.instrument_pipecat(external_agent_id="support-bot")
pipeline = Pipeline([
    transport.input(), stt, user_aggregator, llm, tts,
    transport.output(), tap, assistant_aggregator,
])
worker = PipelineWorker(pipeline, enable_tracing=True, conversation_id=tap.session_id)
```

## Text and chat agents

```python
with client.session(chat_id, external_agent_id="chat-bot") as session:
    session.turn("USER", user_message)
    reply = run_agent(user_message)   # auto-traced model calls join this session
    session.turn("AGENT", reply)

session.end()   # optional: otherwise it builds after an hour idle
```

When the whole function is the session, decorate it. The first argument picks the id from the call.
Use the same flow for SMS with the SMS conversation ID:

```python
@client.session(lambda chat_id, message: chat_id, external_agent_id="chat-bot")
def handle(chat_id: str, message: str) -> str:
    client.turn("USER", message)
    reply = run_agent(message)
    client.turn("AGENT", reply)
    return reply
```

Every span needs a session and an agent: `session()` and the voice integrations set both.
Reopen the same id on the next request and the turns land in one conversation.

## Tools and custom spans

```python
@client.observe(kind="tool")
def lookup_appointment(member_id: str) -> dict: ...

@client.observe(kind="llm", model="mistral-large", provider="mistral_ai")
def ask_mistral(messages: list) -> str: ...

# a model call the SDK does not trace on its own (a provider's native SDK, raw HTTP):
# the span gets the same gen_ai.* attributes as an auto-instrumented call
with client.span("chat mistral-large", kind="llm", model="mistral-large", provider="mistral_ai", input=messages) as span:
    reply = mistral.chat.complete(model="mistral-large", messages=messages)
    span.set_output(reply.choices[0].message.content, finish_reason="stop",
                    input_tokens=reply.usage.prompt_tokens, output_tokens=reply.usage.completion_tokens)
```

### Span kinds

Use `client.span(...)` around a block or `@client.observe(...)` on a synchronous or asynchronous function. Both accept the same kinds. `session.span(...)` works inside an active session too.

| Kind | What Bluejay records |
| --- | --- |
| `llm` | Model, provider and timing. Supply custom token usage through `span.set_output()`. |
| `stt`, `tts` | Speech stage, model, provider and timing. For a stream, keep the span open while consuming it to measure total duration. |
| `tool` | Tool name, arguments, result or error. Use this for retrieval functions. |
| `agent` | The named agent run. Model and tool spans started inside inherit its trace context. |
| `handoff` | Source and destination on successful completion. Use the callback that confirms an asynchronous transfer. |
| Omitted | A named, timed step. |

```python
with client.span("Billing", kind="agent"):
    with client.span("synthesize", kind="tts", provider=provider_name, model=model_name):
        await synthesize_and_play(reply)

@client.observe(kind="handoff", from_agent="Triage", to_agent="Billing")
async def transfer_to_billing():
    await confirm_billing_transfer()
```

A transfer tool can pass `from_agent` and `to_agent` on its existing tool span. Raise on failure or call `span.set_error(...)` before leaving the block. The source and destination are recorded only after successful completion. Existing integrations export native spans once; avoid wrapping a call already traced by the runtime.

## Privacy

For advanced tracing options, create the client and configure tracing explicitly at startup.

```python
client = BluejayClient()
client.init_tracing(redact_pii=True, mask=scrub)
client.instrument_livekit(ctx, session, external_agent_id="support-bot", record_audio=False)
```

## The API

The API is on the same client, one namespace per resource, each with `list`, `get`, `create`,
`update` and `delete` where they apply:

```python
from bluejay import models

client.agents.list()
client.digital_humans.create(digital_humans=[...], simulation_ids=[...])
client.simulations.run(simulation_id=...)
client.simulation_results.list(simulation_run_id)
client.evaluate(external_agent_id="support-bot", start_time_utc=..., participants=[...],
                transcript=[models.Turn(speaker="USER", utterance="hi", start_offset_ms=0, end_offset_ms=500)])
```

See [`src/bluejay/api/reference.md`](src/bluejay/api/reference.md) for every method, and
[SDK_GENERATION.md](SDK_GENERATION.md) for how the API client is generated and released.
