Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Humanloop Python Library

fern shield pypi

The Humanloop Python library provides convenient access to the Humanloop API from Python.

Installation

pip install humanloop

Usage

Instantiate and use the client with the following:

from humanloop import ChatMessage, PromptKernelRequest
from humanloop.client import Humanloop

client = Humanloop(
    api_key="YOUR_API_KEY",
)
client.prompts.log(
    path="persona",
    prompt=PromptKernelRequest(
        model="gpt-4",
        template=[
            ChatMessage(
                role="system",
                content="You are {{person}}. Answer questions as this person. Do not break character.",
            )
        ],
    ),
    messages=[
        ChatMessage(
            role="user",
            content="What really happened at Roswell?",
        )
    ],
    inputs={"person": "Trump"},
)

Async Client

The SDK also exports an async client so that you can make non-blocking calls to our API.

from humanloop import ChatMessage, PromptKernelRequest
from humanloop.client import AsyncHumanloop

client = AsyncHumanloop(
    api_key="YOUR_API_KEY",
)
await client.prompts.log(
    path="persona",
    prompt=PromptKernelRequest(
        model="gpt-4",
        template=[
            ChatMessage(
                role="system",
                content="You are {{person}}. Answer questions as this person. Do not break character.",
            )
        ],
    ),
    messages=[
        ChatMessage(
            role="user",
            content="What really happened at Roswell?",
        )
    ],
    inputs={"person": "Trump"},
)

Exception Handling

All errors thrown by the SDK will be subclasses of ApiError.

import humanloop

try:
    client.prompts.call(...)
except humanloop.core.ApiError as e: # Handle all errors
  print(e.status_code)
  print(e.body)

Advanced

Timeouts

By default, requests time out after 60 seconds. You can configure this with a timeout option at the client or request level.

from humanloop.client import Humanloop

client = Humanloop(
    ...,
    # All timeouts are 20 seconds
    timeout=20.0,
)

# Override timeout for a specific method
client.prompts.call(..., {
    timeout_in_seconds=20.0
})

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retriable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retriable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the max_retries request option to configure this behavior.

client.prompts.call(..., {
     max_retries=1
})

Custom HTTP client

You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies and transports.

import httpx

from humanloop.client import Humanloop

client = Humanloop(...,
    http_client=httpx.Client(
        proxies="http://my.test.proxy.example.com",
        transport=httpx.HTTPTransport(local_address="0.0.0.0"),
    ),
)

Pagination

Paginated requests will return a SyncPager or AsyncPager, which can be used as generators for the underlying object.

from humanloop.client import Humanloop

client = Humanloop(
    api_key="YOUR_API_KEY",
)
response = client.prompts.list(
    size=1,
)
for item in response:
    yield item
# alternatively, you can paginate page-by-page
for page in response.iter_pages():
    yield page

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

Metadata

Release files for humanloop 0.8.0b5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for humanloop 0.8.0b5
File Size Uploaded Provenance
humanloop-0.8.0b5.tar.gz 75.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for humanloop 0.8.0b5
File Interpreter ABI Platform
humanloop-0.8.0b5-py3-none-any.whl Python 3 none any Details

Total release size: 247.4 kB

Release files / humanloop-0.8.0b5.tar.gz

Download URL humanloop-0.8.0b5.tar.gz
Size 75.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e5c3f933ab5ba2e4c03ced30b934bb362e431ec98f6512e09dd73e12b4f85480
BLAKE2b-256 checksum
How to use checksums
8b78bda337a33324a7688a5bcbeefb244397ae2e057208412044d963924a2af0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.8.18 Linux/5.15.0-1067-azure

Release files / humanloop-0.8.0b5-py3-none-any.whl

Download URL humanloop-0.8.0b5-py3-none-any.whl
Size 171.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8012c86c6fbd52a65efce3ac87dde19e29854ddace4b45d277be9ef17df549ae
BLAKE2b-256 checksum
How to use checksums
aa470964089d12e65bb37e39ffbec2225cd4d3b61de4f222ab2ddd698dae7e4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.8.18 Linux/5.15.0-1067-azure

Security for humanloop 0.8.0b5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page