Skip to main content

Respan instrumentation plugin for OpenRouter's OpenAI-compatible Python usage

Project description

Respan OpenRouter instrumentation

Trace OpenRouter calls made through the OpenAI-compatible Python client with Respan.

OpenRouter's Python usage is OpenAI-compatible, so this package delegates SDK patching to respan-instrumentation-openai and adds a package-local processor that normalizes those spans as OpenRouter spans before export.

Install

pip install respan-ai respan-instrumentation-openrouter openai

Environment

Variable Required Description
RESPAN_API_KEY Yes Respan API key for trace export.
RESPAN_BASE_URL No Defaults to https://api.respan.ai/api.
OPENROUTER_API_KEY Yes OpenRouter API key for direct OpenRouter calls.
OPENROUTER_BASE_URL No Defaults to https://openrouter.ai/api/v1.
OPENROUTER_MODEL No Defaults to openai/gpt-4o-mini.

The examples can also run through a Respan gateway configuration by providing OPENROUTER_USE_RESPAN_GATEWAY=true, RESPAN_GATEWAY_API_KEY, RESPAN_GATEWAY_BASE_URL, and RESPAN_MODEL. Without direct OpenRouter or explicit gateway configuration, the example set uses a local OpenAI-compatible mock so instrumentation and Respan export can still be verified.

Usage

import os

from openai import OpenAI
from respan import Respan, workflow
from respan_instrumentation_openrouter import OpenRouterInstrumentor

respan = Respan(
    api_key=os.environ["RESPAN_API_KEY"],
    base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
    instrumentations=[OpenRouterInstrumentor()],
)

client = OpenAI(
    api_key=os.environ["OPENROUTER_API_KEY"],
    base_url=os.getenv("OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1"),
)


@workflow(name="openrouter_chat_example")
def run_chat() -> str:
    response = client.chat.completions.create(
        model=os.getenv("OPENROUTER_MODEL", "openai/gpt-4o-mini"),
        messages=[
            {
                "role": "user",
                "content": "Reply with one concise sentence about observability.",
            }
        ],
    )
    return response.choices[0].message.content or ""


try:
    print(run_chat())
finally:
    respan.flush()
    respan.shutdown()

OpenRouterInstrumentor(normalize_all_openai_spans=True) is the default because the package is intended for OpenRouter-specific OpenAI-compatible clients. Set normalize_all_openai_spans=False if the same process also emits regular OpenAI spans and only spans with OpenRouter URL markers should be rewritten.

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

respan_instrumentation_openrouter-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file respan_instrumentation_openrouter-0.1.0.tar.gz.

File metadata

File hashes

Hashes for respan_instrumentation_openrouter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 06d0fb12c01ec14343e57da492edc0ace01f1265a17d03ae60610235c84f8661
MD5 9bdb86f5495f52e239cce03123a185d2
BLAKE2b-256 e5150d66a40dd28b22caedc48ff41d3408c519b6c5dd41e274e0a23a0481a99e

See more details on using hashes here.

File details

Details for the file respan_instrumentation_openrouter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for respan_instrumentation_openrouter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b269bcde7ff46ca40095da3f221cf8d9572373f108c6dfba8fc9abec7f3b6fd6
MD5 0fadf2ac2e120920f57848bc3814d7de
BLAKE2b-256 72e5bfbe3e02bbc0bfe20bf488eae6aea95a2ba8050f3a8df5825c5def157ede

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