Skip to main content
The Latitude Python SDK provides a convenient way to interact with the Latitude platform from your Python applications.

Installation

The Latitude SDK is compatible with Python 3.9 or higher.

Authentication and Initialization

Import the SDK and initialize it with your API key. You can generate API keys in your Latitude project settings under “API Access”.
You can also provide additional options during initialization:
Both project_id and version_uuid options can be overridden on a per-method basis when needed.

Examples

Check out our Examples section for more examples of how to use the Latitude SDK.

SDK Usage

The Latitude Python SDK is an async library by design. This means you must use it within an async event loop, such as FastAPI or Async Django. Another option is to use the built-in asyncio library.

SDK Structure

The Latitude SDK is organized into several namespaces:
  • prompts: Methods for managing and running prompts
  • runs: Methods for managing active runs
  • logs: Methods for pushing logs to Latitude
  • evaluations: Methods for pushing evaluation results to Latitude
  • projects: Methods for managing projects
  • versions: Methods for managing project versions

Prompt Management

Get a Prompt

To retrieve a specific prompt by its path:

Get All Prompts

To retrieve all prompts in your project:

Get or Create a Prompt

To get an existing prompt or create a new one if it doesn’t exist:
You can also provide the content when creating a new prompt:

Delete a Prompt

To delete a prompt from a draft version:
This soft-deletes the document. The deletion only works on draft (non-merged) commits.

Version Management

Get All Versions

To retrieve all versions from a project:
You can also specify a different project ID:

Running Prompts

Non-Streaming Run

Execute a prompt and get the complete response once generation is finished:

Handling Streaming Responses

For real-time applications (like chatbots), use streaming to get response chunks as they are generated:

Using Tools with Prompts

You can provide tool handlers that the model can call during execution:

Chat with a Prompt

Follow the conversation of a runned prompt:
Messages follow the PromptL format. If you’re using a different method to run your prompts, you’ll need to format your messages accordingly.

Running a Prompt in the Background

For long-running prompts, such as large Agent systems, that you don’t need to wait for, use background runs:

Run Management

Stop a Run

Stop an active conversation that is currently running:

Attach to a Run

Attach to an active conversation to receive its ongoing output:

Rendering Prompts

Prompt Rendering

Render a prompt locally without running it:

Chain Rendering

Render a chain of prompts locally:

Logging

Creating Logs

Push a log to Latitude manually for a prompt:

Evaluations

Annotate a log

Push an evaluation result (annotate) to Latitude:

Complete Method Reference

Initialization

Prompts Namespace

Runs Namespace

Projects Namespace

Logs Namespace

Evaluations Namespace

Versions Namespace

Error Handling

The SDK raises ApiError instances when API requests fail. You can catch and handle these errors:

Logging Features

  • Automatic Logging: All runs through latitude.prompts.run() are automatically logged in Latitude, capturing inputs, outputs, performance metrics, and trace information.
  • Custom Identifiers: Use the optional custom_identifier parameter to tag runs for easier filtering and analysis in the Latitude dashboard.
  • Response Identification: Each response includes identifying information like uuid that can be used to reference the specific run later.

Further Information