# Run with Python SDK

Use the official `fetch-hive-sdk` package when you want to send a message to an agent from Python. The SDK wraps the public [`POST /v1/agent/invoke`](/api-reference/invoke-agent.md) endpoint, handles authentication, supports streaming and multimodal inputs, and exposes both synchronous and `asyncio` variants.

## Installation

```bash
pip install fetch-hive-sdk
```

The SDK requires Python 3.9+ and uses `httpx` under the hood.

## Authentication

Set the `FETCH_HIVE_API_KEY` environment variable to your workspace API key (the SDK reads it automatically):

```bash
export FETCH_HIVE_API_KEY=fhk_...
```

```python
from fetch_hive_sdk import FetchHive

client = FetchHive()
```

Or pass the key explicitly:

```python
client = FetchHive(api_key="fhk_...")
```

See [API Keys](/your-workspace/api-keys.md) for how to create and rotate keys.

## Basic example

Send a message to an agent and read the final response:

```python
from fetch_hive_sdk import FetchHive

client = FetchHive()

reply = client.invoke_agent(
    agent="AGENT_UUID",
    message="Summarize the latest AI infrastructure trends",
)

print(reply["response"])
```

See the [non-streaming response shape](/api-reference/invoke-agent.md#response).

## Method reference

| Argument     | Type         | Required | Description                                                                                                                                      |
| ------------ | ------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `agent`      | `str`        | Yes      | The agent ID                                                                                                                                     |
| `message`    | `str`        | Yes      | The message you want to send                                                                                                                     |
| `thread_id`  | `str`        | No       | An arbitrary string identifying a persistent conversation thread. Fetch Hive creates the thread on first use and resumes it on subsequent calls. |
| `messages`   | `list[dict]` | No       | Caller-managed conversation history. Each item: `{"role": "user" \| "assistant" \| "system", "content": str}`.                                   |
| `image_urls` | `list[str]`  | No       | HTTPS image URLs attached to the current `message` for multimodal inputs                                                                         |
| `user`       | `str`        | No       | Opaque caller identifier surfaced in [User Tracking](/user-tracking/user-tracking.md)                                                            |

The SDK injects `streaming: false` for `invoke_agent`. To stream, use `invoke_agent_stream` (below).

## Handling the response

```python
reply = client.invoke_agent(agent="AGENT_UUID", message="Hello")

print(reply["response"])      # final text
print(reply["model"])         # model identifier
print(reply["usage"])         # token usage breakdown
print(reply["request_id"])    # use this to look up the run in Logs
print(reply.get("tool_calls"))  # tool invocations made during the run
```

## Streaming

Use `invoke_agent_stream` to receive Server-Sent Events as they arrive. The method returns a generator that yields parsed event dicts:

```python
for chunk in client.invoke_agent_stream(
    agent="AGENT_UUID",
    message="Summarize the latest AI infrastructure trends",
    thread_id="user-456-support-session",
):
    if chunk.get("type") == "response":
        print(chunk.get("response", ""), end="", flush=True)
    elif chunk.get("type") == "tool":
        print(f"\n[Calling tool: {chunk.get('tool')}]")
    elif chunk.get("type") == "usage":
        print("\n\nUsage:", chunk["usage"])
```

The stream yields the same event types documented in [Invoke Agent → Response](/api-reference/invoke-agent.md#response): `summary` (when auto-summarization fires), `reasoning`, `response`, `tool`, and a final `usage` event.

### Async streaming

For `asyncio` applications, use `ainvoke_agent_stream`. It has the same arguments but returns an async iterator:

```python
import asyncio
from fetch_hive_sdk import FetchHive

async def main():
    client = FetchHive()
    async for chunk in client.ainvoke_agent_stream(
        agent="AGENT_UUID",
        message="Hello",
        thread_id="user-456-support-session",
    ):
        if chunk.get("type") == "response":
            print(chunk.get("response", ""), end="", flush=True)

asyncio.run(main())
```

## Multi-turn conversations

### Persistent threads

Pass any string as `thread_id` and Fetch Hive will create the thread on the first call and resume it on subsequent calls with the same value:

```python
client.invoke_agent(
    agent="AGENT_UUID",
    message="What are the main AI infrastructure trends right now?",
    thread_id="user-456-support-session",
)

client.invoke_agent(
    agent="AGENT_UUID",
    message="Which of those trends have the most enterprise adoption?",
    thread_id="user-456-support-session",
)
```

### Stateless history

Manage state yourself by passing the previous turns in `messages`. Fetch Hive uses the supplied history for context but does not persist it:

```python
client.invoke_agent(
    agent="AGENT_UUID",
    message="Which of those trends have the most enterprise adoption?",
    messages=[
        {"role": "user", "content": "What are the main AI infrastructure trends right now?"},
        {"role": "assistant", "content": "Teams are focusing on evals, tool routing, and observability."},
    ],
)
```

## Multimodal inputs

Attach images to the current message with `image_urls`:

```python
result = client.invoke_agent(
    agent="vision-agent",
    message="Describe this image",
    image_urls=["https://example.com/photo.jpg"],
)
print(result["response"])
```

URLs must start with `https://`.

## Configuration

| Option     | Default                        | Description                     |
| ---------- | ------------------------------ | ------------------------------- |
| `api_key`  | `FETCH_HIVE_API_KEY` env var   | Bearer token from the dashboard |
| `base_url` | `https://api.fetchhive.com/v1` | Override the API base URL       |
| `timeout`  | `120`                          | Request timeout in seconds      |

```python
client = FetchHive(
    api_key="fhk_...",
    base_url="https://api.fetchhive.com/v1",
    timeout=120,
)
```

## Errors

Non-2xx responses raise an `httpx.HTTPStatusError` with the status code and response body:

```python
import httpx

try:
    reply = client.invoke_agent(agent="AGENT_UUID", message="Hello")
except httpx.HTTPStatusError as exc:
    print("Fetch Hive returned", exc.response.status_code, exc.response.text)
```

See [Errors and Rate Limits](/api-reference/errors-and-rate-limits.md) for status code meanings.

## Links

* [Package on PyPI](https://pypi.org/project/fetch-hive-sdk/)
* [Source on GitHub](https://github.com/Fetch-Hive/python-sdk)

## Next steps

* [Run with API](/agents/run-with-api.md) - The same flow with cURL
* [Run with Node.js SDK](/agents/run-with-nodejs-sdk.md)
* [Run with Ruby SDK](/agents/run-with-ruby-sdk.md)
* [Run with PHP SDK](/agents/run-with-php-sdk.md)
* [Invoke Agent](/api-reference/invoke-agent.md) - Full endpoint reference


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fetchhive.com/agents/run-with-python-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
