Skip to main content

Orchid Thin SDK for environment routing, transport patching, and proxy control

Project description

Orchid Python SDK

The Orchid Python SDK is a lightweight, thin SDK designed to work seamlessly with Orchid - the Orchestration Interactive Debugger.

It automatically intercepts outgoing LLM and external API requests (e.g., OpenAI, Anthropic, Gemini, Vertex AI, SerpAPI, etc.) and routes them through the local Orchid Proxy. This enables full request capturing, visualization, interactive debugging, and mock replay capabilities in unit tests or local development environments.


Installation

Install the SDK via pip:

pip install orchid-sdk

Quickstart

1. Initialization

Call orchid.init() at the entry point of your application (before instantiating any LLM clients).

import orchid

# Initialize the Orchid environment and patch network transports
orchid.init()

# Now, standard client libraries will automatically route through the Orchid proxy!
import openai
client = openai.OpenAI()

2. Session Contexts

Use the orchid.session context manager to group API exchanges under a specific session ID and mode.

import orchid

# Initialize the SDK
orchid.init()

# Intercept and capture requests in this block under a custom session name
with orchid.session("user-onboarding-flow", mode="capture"):
    # Outgoing LLM calls here will be captured under session ID 'user-onboarding-flow'
    response = client.chat.completions.create(
        model="gpt-5.2",
        messages=[{"role": "user", "content": "Hello!"}]
    )

Supported modes are:

  • "capture": Intercepts and records all outgoing requests.
  • "replay": Intercepts requests and serves mocked responses from previously recorded exchanges.
  • "passthrough": Bypasses the proxy database logging (requests still pass through but are not recorded/mocked).

Capture & Replay Decorator (@replay)

The @replay decorator automates capturing and replaying HTTP exchanges. It is perfect for unit tests and local iteration, ensuring test suites are deterministic and do not make expensive, slow, or volatile external network calls.

from orchid import replay

# The decorator wraps both synchronous and asynchronous functions
@replay("tests/fixtures/test_user_greeting.json")
def test_user_greeting():
    # If ORCHID_RECORD=1: This will execute the API call and save results to the JSON fixture file.
    # If ORCHID_RECORD=0 or unset: This will mock the API call using the contents of the fixture file.
    response = client.chat.completions.create(
        model="gpt-5.2",
        messages=[{"role": "user", "content": "Say hello!"}]
    )
    assert "hello" in response.choices[0].message.content.lower()

Note: In async contexts, the decorator automatically offloads synchronous file and query service operations to a background executor (loop.run_in_executor) to prevent blocking the asyncio event loop.


Orchid Control Client

For programmatic control over recorded fixtures, you can use the OrchidControlClient to manually check health, export, or import fixtures.

from orchid import OrchidControlClient

# Create the client pointing to the control port (default: 4321)
control_client = OrchidControlClient()

# Check if the query service is active
if control_client.check_health():
    # Export a recorded session to a local JSON file
    control_client.export_fixture(session_id="my-session-123", path="fixtures/session_data.json")

    # Import a local JSON fixture database back into the proxy
    control_client.import_fixture(path="fixtures/session_data.json")

How It Works (Auto-Instrumentation)

When you run orchid.init(), the SDK dynamically monkeypatches the standard request-sending hooks of the following HTTP clients:

  1. httpx (httpx.Client.send and httpx.AsyncClient.sendNote: This automatically intercepts libraries like openai and anthropic which use httpx as their underlying HTTP transport).
  2. requests (requests.Session.request)
  3. aiohttp (aiohttp.ClientSession._request)
  4. google-cloud-aiplatform (Vertex AI client classes are forced to use REST transports, which in turn use patched requests or aiohttp clients).

Fail-Soft Fallback

The SDK is built to be resilient. During initialization, the SDK performs a fast health check on the Orchid Query service.

  • If the proxy or control service is offline, the SDK silently falls back to direct routing. None of the client patches are active, and environment base URLs are not modified.
  • If a patch is active but the proxy goes offline mid-session, transport layers catch connection failures, log a warning, case-insensitively purge internal Orchid headers (preventing key leakage), and seamlessly retry the request directly to the upstream public API.

Configuration Variables

Configure the SDK using the following environment variables:

Variable Default Description
ORCHID_PROXY_URL http://127.0.0.1:4320/v1 Base URL of the Orchid interceptor proxy.
ORCHID_QUERY_URL http://127.0.0.1:4321 Base URL of the Orchid Query and Control service.
ORCHID_API_KEY None API key required to authenticate to the Orchid Proxy and Control Client (injected as X-Orchid-Api-Key).
ORCHID_RECORD 0 Set to 1, true, or yes to run @replay decorators in capture/record mode.
ORCHID_CAPTURE_DOMAINS None Comma-separated list of domains to proxy. Use * to capture all outgoing HTTP requests.
ORCHID_IGNORE_DOMAINS None Comma-separated list of domains to explicitly bypass (ignored during capture).
ORCHID_FLUSH_SLEEP 0.2 Delay in seconds before exporting a captured session to allow async logs to flush.
ORCHID_BYPASS_HEALTHCHECK False Set to True to force-apply monkeypatches without performing the query service handshake.

License

This project is licensed under the Apache License 2.0.

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

orchid_sdk-0.3.3.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

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

orchid_sdk-0.3.3-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file orchid_sdk-0.3.3.tar.gz.

File metadata

  • Download URL: orchid_sdk-0.3.3.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for orchid_sdk-0.3.3.tar.gz
Algorithm Hash digest
SHA256 61290b77a92f308e0af6a107a0855ca7c850f3d79f7ff807beb57b34e6e62d49
MD5 4e272f9d7f853715e343ab543d56549a
BLAKE2b-256 ca6ef0e938575bab8977ce9291a7435d7159b4609bdd1ffc9e70cb7f51169ba8

See more details on using hashes here.

File details

Details for the file orchid_sdk-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: orchid_sdk-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for orchid_sdk-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5727f09b326cffd07ebb7310bc0692223716633d9079b379c0f6db6094348eaa
MD5 ee5f0193977a1ff78b2d7a27cde79195
BLAKE2b-256 3b404246f047af10c6b34ed32e58f7ee83f7a0e82e2f9ab8c9f0578ffc4fe607

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