A thin client for tracing LLM calls
Project description
Tropir Session Utilities
This utility helps manage session IDs for tracking requests and automatically injects relevant headers into your HTTP requests.
Installation
Install package using pip:
pip install tropir
Purpose
When making requests to backend services (like LLM APIs or tracking endpoints), it's often useful to include:
- A Session ID (
X-Session-ID): To group related requests within a single user interaction or process run. - An API Key (
X-TROPIR-API-KEY): To authenticate requests with a specific service (like a Tropir backend).
This utility provides a simple way to manage session IDs and automatically add these headers to your request dictionaries.
Basic Usage: Automatic Header Injection
The primary way to use this utility is via the prepare_request_headers function. It takes your existing headers dictionary for an HTTP request and modifies it in place.
import requests
import os
from tropir.session_utils import prepare_request_headers
# Set the Tropir API key in your environment variables (optional)
# export TROPIR_API_KEY='your_api_key_here'
# Example API call
api_url = "https://example.com/api/resource"
payload = {"data": "some_value"}
# Initial headers (e.g., for authentication or content type)
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer some_other_token"
}
# Prepare the headers: This adds X-Session-ID and potentially X-TROPIR-API-KEY
prepare_request_headers(headers)
# Now the 'headers' dictionary contains the added keys:
# {
# "Content-Type": "application/json",
# "Authorization": "Bearer some_other_token",
# "X-Session-ID": "<generated_or_thread_local_uuid>",
# "X-TROPIR-API-KEY": "your_api_key_here" # (If env var was set)
# }
print(f"Sending request with headers: {headers}")
response = requests.post(api_url, headers=headers, json=payload)
# Handle response...
print(response.status_code)
How it works:
X-Session-ID:- It first checks if a session ID has been explicitly set for the current thread using
set_session_id()(see Advanced Usage). - If no thread-specific ID is found, it falls back to a default UUID generated once when the process started. This ensures all requests within the same process run (that don't have overrides) share the same default session ID.
- It first checks if a session ID has been explicitly set for the current thread using
X-TROPIR-API-KEY:- It checks for the environment variable
TROPIR_API_KEY. - If the environment variable is set, its value is added to the headers.
- If not set, the header is simply omitted.
- It checks for the environment variable
Advanced Usage: Manual Session ID Control
While prepare_request_headers is sufficient for most use cases, you can manually control the session ID on a per-thread basis if needed:
from tropir.session_utils import get_session_id, set_session_id, clear_session_id
import threading
def worker_task(custom_id):
print(f"[{threading.current_thread().name}] Default Session ID: {get_session_id()}")
# Set a custom session ID for this thread
set_session_id(custom_id)
print(f"[{threading.current_thread().name}] Thread-specific Session ID: {get_session_id()}")
# ... make requests using prepare_request_headers ...
# Headers will now use 'custom_id' for X-Session-ID
# Clear the thread-specific ID, falling back to the default
clear_session_id()
print(f"[{threading.current_thread().name}] Session ID after clear: {get_session_id()}")
# Create and run threads with custom IDs
thread1 = threading.Thread(target=worker_task, args=("session-abc",), name="Worker-1")
thread2 = threading.Thread(target=worker_task, args=("session-xyz",), name="Worker-2")
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(f"[Main Thread] Session ID: {get_session_id()}") # Will show the process default ID
set_session_id(your_id): Overrides the session ID for the current thread.get_session_id(): Retrieves the current effective session ID (thread-local or process default).clear_session_id(): Removes the thread-local override, causingget_session_id()to return the process default again for this thread.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tropir-2.4.tar.gz.
File metadata
- Download URL: tropir-2.4.tar.gz
- Upload date:
- Size: 33.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
002d4ff499a9a487223611cdebb4063b7f24552c2da3091acde19364721b68d6
|
|
| MD5 |
06395d5fc6df340302cfdd283bef5b08
|
|
| BLAKE2b-256 |
43d1bdaf38be3f788e30b7e6fb60da8a8c1a6b25ed9e5771d84f08c0597f2913
|
File details
Details for the file tropir-2.4-py3-none-any.whl.
File metadata
- Download URL: tropir-2.4-py3-none-any.whl
- Upload date:
- Size: 33.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b5d3fc9ba6b7a99442faa498428695802b4a03273260bf4282c0c1528559d33
|
|
| MD5 |
930a76848cc16bd0cad43814c2bf99ef
|
|
| BLAKE2b-256 |
977e9c051a97f63beff7940d6be9a1ec3bcf1329379fdeb11f9deae82889d259
|