DocsSDKPython

Using Lamatic SDK with Python

This guide demonstrates how to integrate the Lamatic Python SDK into your Python applications to execute flows.

This SDK is in early release (v0.3.0). It has the same features as the JavaScript SDK and Go SDK: running flows, streaming, and waiting for async results. None of the SDKs run agents: the platform’s agent API is deprecated, so use flows instead.

Installation

Requires Python 3.10+. Install with pip:

pip install lamatic

Getting Started

To use the Lamatic Python SDK, you’ll need:

  • An API key or access token
  • Your project ID
  • The Lamatic API endpoint

Basic Setup

from lamatic import Lamatic
 
lamatic = Lamatic(
    endpoint="https://yourproject.lamatic.dev/graphql",
    project_id="your-project-id",
    api_key="your-api-key",
)

Authentication

The SDK supports two authentication methods:

from lamatic import Lamatic
 
lamatic = Lamatic(
    endpoint="https://yourproject.lamatic.dev/graphql",
    project_id="your-project-id",
    api_key="your-api-key",
)

You must provide either api_key or access_token, but not both, the constructor raises ValueError if neither is set.

Executing Flows

Execute a workflow synchronously with execute_flow:

response = lamatic.execute_flow(
    flow_id="your-flow-id",
    payload={"prompt": "Hello, Lamatic!"},
)
 
print(response.status)   # "success"
print(response.result)
print(response.message)

Async Usage

For async applications, use async_execute_flow instead:

import asyncio
from lamatic import Lamatic
 
lamatic = Lamatic(
    endpoint="https://yourproject.lamatic.dev/graphql",
    project_id="your-project-id",
    api_key="your-api-key",
)
 
async def main():
    response = await lamatic.async_execute_flow(
        flow_id="your-flow-id",
        payload={"prompt": "Hello from async!"},
    )
    print(response.status)
    print(response.result)
 
asyncio.run(main())

Streaming Responses

Use execute_flow_token_stream to show LLM output as it is generated, instead of waiting for the whole flow to finish. It runs the flow and yields one event at a time. Streaming requires lamatic v0.2.0 or later.

for event in lamatic.execute_flow_token_stream("your-flow-id", {"prompt": "Write a short story"}):
    if event.type == "token":
        print(event.token, end="", flush=True)  # one piece of text
    elif event.type == "final":
        print("\nFull text:", event.text)
    elif event.type == "error":
        print("\nStream failed:", event.message)

In async code, use async_execute_flow_token_stream with async for:

async for event in lamatic.async_execute_flow_token_stream("your-flow-id", {"prompt": "Write a short story"}):
    if event.type == "token":
        print(event.token, end="", flush=True)

Events

EventFieldsWhen
tokentoken, node_id, rawAn LLM or RAG node produced the next piece of text
nodenode_id, output, rawA node finished; output is its full output
finalresult, text, text_by_node, rawThe flow finished. This is always the last event of a successful stream
errormessage, node_id, rawSomething failed. The stream ends if the failure was fatal
  • final.text is every token joined together. Use it instead of reading generatedResponse from a node’s output, because the flow’s result only holds what its response node returns.
  • final.text_by_node splits the text by node, for flows with more than one LLM or RAG node.
  • raw is the original server frame. On the node event of an LLM node, raw["data"]["_meta"] holds token counts and cost.

Only LLM and RAG nodes stream token by token. Every other node reports once, as a single node event when it finishes.

Stopping a stream

In sync code, break out of the loop to stop generation; the connection is closed for you.

In async code, wrap the stream in contextlib.aclosing. Python does not close an async generator straight away when you break out of an async for loop, so without it the connection can stay open until the generator is garbage-collected:

from contextlib import aclosing
 
async with aclosing(lamatic.async_execute_flow_token_stream("your-flow-id", {"prompt": "Write a long story"})) as stream:
    async for event in stream:
        if event.type == "token":
            print(event.token, end="", flush=True)
        if user_pressed_stop():
            break  # aclosing closes the stream and its connection here

Cancelling the task that consumes the stream also stops it.

Handling stream errors

Failures arrive as an error event, not as a raised exception. That covers invalid credentials, network errors and a connection that drops before the flow finishes.

Async Flows

A flow whose API trigger is set to run asynchronously answers at once with only a request ID (result == {"requestId": ...}) and keeps running in the background. The SDK can wait for the result for you.

The methods in this section need lamatic v0.3.0 or later. In earlier versions check_status failed on every call with a GraphQL error.

Run a flow and wait for the result

execute_flow_poll (sync) and async_execute_flow_poll (async) run a flow and, if it was queued, poll until it finishes. A realtime flow’s response comes back unchanged, so they are safe to use for any flow:

response = lamatic.execute_flow_poll(
    "your-flow-id",
    {"prompt": "Write a long report"},
    poll_interval=5,    # seconds between polls, default 15
    poll_timeout=300,   # max wait time in seconds, default 900
)
 
if response.status == "success":
    print(response.result)
else:
    print(response.status, response.message)

Poll a request ID you already have

response = lamatic.check_status(
    request_id="your-request-id",
    poll_interval=5,    # seconds between polls, default 15
    poll_timeout=120,   # max wait time in seconds, default 900
)
 
print(response.status)
print(response.result)

What polling returns

statusstatus_codeMeaning
success200The flow finished. result is its output, the same shape a realtime run returns.
failed200A node failed. message is the error, and result["failedNode"] names the node.
error404The request ID was still unknown after 60 seconds of polling.
error408The run had not finished when poll_timeout ran out.

A 408 means only that polling gave up: the flow isn’t cancelled and may still finish in the background. Call check_status again later with the same request ID.

Updating Access Token

If you’re using access token authentication, update it at runtime with update_access_token:

lamatic.update_access_token("new-access-token")

Response Structure

execute_flow, execute_flow_poll, check_status and their async variants all return a LamaticResponse:

@dataclass
class LamaticResponse:
    status: str                        # "success" | "error" | "failed"
    result: dict | None
    message: str | None = None
    status_code: int | None = None

Example

response = lamatic.execute_flow("your-flow-id", {"prompt": "hey, how are you?"})
 
if response.status == "success":
    print("Result:", response.result)
else:
    print("Failed:", response.message)

Error Handling

execute_flow and async_execute_flow raise an exception on network failures (they don’t catch and swallow errors), so wrap calls in a try/except block:

try:
    response = lamatic.execute_flow(flow_id, payload)
    if response.status == "success":
        print("Success:", response.result)
    else:
        print("Execution failed:", response.message)
except Exception as e:
    print("SDK error:", e)

check_status, execute_flow_poll and their async variants behave differently once polling starts: they catch errors internally and return a LamaticResponse with status="error" instead of raising, so you can check response.status without a try/except.

Environment Variables

For better security, keep credentials out of source code:

import os
from lamatic import Lamatic
 
lamatic = Lamatic(
    endpoint=os.environ["LAMATIC_PROJECT_ENDPOINT"],
    project_id=os.environ["LAMATIC_PROJECT_ID"],
    api_key=os.environ["LAMATIC_PROJECT_API_KEY"],
)

Resources

Support

For help and support with the Python SDK:

Was this page useful?

Subscribe to updates