Python SDK

Installation

poetry

poetry add autoblocksai

pip

pip install autoblocksai

AutoblocksTracer

The AutoblocksTracer class is used to send events to Autoblocks. The only required argument is your ingestion key:

Python

import os

from autoblocks.tracer import AutoblocksTracer

tracer = AutoblocksTracer(os.environ["AUTOBLOCKS_INGESTION_KEY"])

There are additional, optional arguments that you can use to further configure the tracer:

  • Name
    trace_d
    Type
    Optional[str]
    Description

    The trace_id for all events sent through the tracer instance.

  • Name
    properties
    Type
    Optional[Dict]
    Description

    The properties that will be sent for all events sent through the tracer instance.

  • Name
    timeout
    Type
    timedelta | undefined
    Description

    The timeout for each send_event call, which is a POST request to the Autoblocks ingestion API. Default value is timedelta(seconds=5).

Python

from datetime import timedelta

tracer = AutoblocksTracer(
  os.environ["AUTOBLOCKS_INGESTION_KEY"],
  trace_id="my-trace-id",
  properties={"source": "my-app"},
  timeout=timedelta(seconds=1),
)

send_event

Send an event to Autoblocks.

Python

event = tracer.send_event("ai.request", properties={"provider": "openai", "prompt": "Hello!"})
print(f"The trace_id of the event is: {event.trace_id}")

Arguments

  • Name
    message
    Type
    str
    Description

    The name of the event.

  • Name
    trace_id
    Type
    Optional[str]
    Description

    Used to group related events together. If not provided, defaults to a random UUID.

  • Name
    properties
    Type
    Optional[dict]
    Description

    Custom properties to attach to the event. The values in this dictionary can be anything, including other dictionaries.

  • Name
    span_id
    Type
    Optional[str]
    Description

    Within a trace, connect related events together using span IDs. This is useful to connect request and response events together, for example.

  • Name
    parent_span_id
    Type
    Optional[str]
    Description

    Establish parent / child relationships between your spans using parent span IDs. For example, if some events represent work being done on behalf of a higher level span in your trace, you should set that span's ID as the parent span ID for those events.

  • Name
    timestamp
    Type
    Optional[str]
    Description

    An ISO date time string of when the event happened. If not provided, defaults to the current time.

Returns

Returns a dataclass containing:

  • Name
    trace_id
    Type
    Optional[str]
    Description

    The trace_id associated with the event. Returns None if the event failed to be delivered.

set_trace_id

Use this method to set the trace_id for all subsequent calls of send_event.

Python

tracer.set_trace_id('my-trace-id')

Arguments

  • Name
    trace_id
    Type
    str
    Description

    The trace_id to set.

set_properties

Use this method to set the properties for all subsequent calls of send_event.

Python

tracer.set_properties({"x": 1, "y": 2})  # --> {"x": 1, "y": 2}
tracer.set_properties({"x": 2})  # --> {"x": 2}

Arguments

  • Name
    properties
    Type
    dict
    Description

    The properties to set.

update_properties

Use this method to update the properties for all subsequent calls of send_event.

Python

tracer.update_properties({"x": 1, "y": 2})  # --> {"x": 1, "y": 2}
tracer.update_properties({"x": 2})  # --> {"x": 2, "y": 2}

Arguments

  • Name
    properties
    Type
    dict
    Description

    The properties to update.

AutoblocksAPIClient

The AutoblocksAPIClient allows you to interact with the Autoblocks REST API. The only required argument is your API key.

Python

import os

from autoblocks.api.client import AutoblocksAPIClient

client = AutoblocksAPIClient(os.environ["AUTOBLOCKS_API_KEY"])

There are additional, optional arguments that you can use to further configure the client:

  • Name
    timeout
    Type
    timedelta | undefined
    Description

    The timeout for each HTTP request to the Autoblocks REST API. Default value is timedelta(seconds=10).

Python

from datetime import timedelta

client = AutoblocksAPIClient(
  os.environ["AUTOBLOCKS_API_KEY"],
  timeout=timedelta(seconds=30),
)

See the REST API documentation for more details on all of the endpoints exposed through this client.

LangChain Callback

See the section on LangChain in our integration docs.