Skip to content

Client Class

The Client class provides the core functionality for interacting with the Rizk API. It extends Traceloop’s client with Rizk-specific features.

Class Definition

class Client:
"""Client for interacting with the Rizk API."""

Instance Methods

init

def __init__(
api_key: str,
app_name: str,
opentelemetry_endpoint: Optional[str] = None,
**kwargs
) -> None

Initialize the Rizk client.

Parameters

ParameterTypeDefaultDescription
api_keystr-Rizk API key
app_namestr-Name of the application
opentelemetry_endpointOptional[str]NoneOpenTelemetry endpoint URL
**kwargs--Additional configuration options

Example

client = Client(
api_key="your_api_key",
app_name="my_app",
opentelemetry_endpoint="http://localhost:4317"
)

set_association_properties

def set_association_properties(
organization_id: str,
project_id: str,
conversation_id: Optional[str] = None,
**kwargs
) -> None

Set association properties for telemetry and tracking.

Parameters

ParameterTypeDefaultDescription
organization_idstr-Organization identifier
project_idstr-Project identifier
conversation_idOptional[str]NoneConversation identifier
**kwargs--Additional properties

Example

client.set_association_properties(
organization_id="org123",
project_id="proj456",
conversation_id="conv789",
user_id="user123"
)

get_association_properties

def get_association_properties() -> Dict[str, Any]

Get current association properties.

Returns

  • Dict[str, Any]: Current association properties

Example

props = client.get_association_properties()
print(f"Organization: {props['organization_id']}")

clear_association_properties

def clear_association_properties() -> None

Clear all association properties.

Example

client.clear_association_properties()

send_telemetry

async def send_telemetry(
data: Dict[str, Any],
context: Optional[Dict[str, Any]] = None
) -> None

Send telemetry data to the Rizk API.

Parameters

ParameterTypeDefaultDescription
dataDict[str, Any]-Telemetry data to send
contextOptional[Dict[str, Any]]NoneAdditional context

Example

await client.send_telemetry({
"event_type": "policy_violation",
"policy_id": "content_moderation",
"severity": "high"
})

get_tracer

def get_tracer(name: str) -> Tracer

Get an OpenTelemetry tracer.

Parameters

ParameterTypeDescription
namestrName of the tracer

Returns

  • Tracer: OpenTelemetry tracer instance

Example

tracer = client.get_tracer("my_component")

get_metrics

def get_metrics() -> Metrics

Get metrics collection instance.

Returns

  • Metrics: Metrics collection instance

Example

metrics = client.get_metrics()
metrics.counter("policy_checks").add(1)

Properties

PropertyTypeDescription
api_keystrRizk API key
app_namestrApplication name
opentelemetry_endpointOptional[str]OpenTelemetry endpoint URL
association_propertiesDict[str, Any]Current association properties
tracer_providerTracerProviderOpenTelemetry tracer provider
metrics_providerMetricsProviderOpenTelemetry metrics provider

Error Handling

The client handles various types of errors:

try:
await client.send_telemetry(data)
except APIError as e:
print(f"API error: {e.message}")
except ConnectionError as e:
print(f"Connection error: {e.message}")
except ValidationError as e:
print(f"Validation error: {e.message}")