API and SDK
Everything Foundry UI does goes through the same REST APIs you can call from code. Each service publishes an OpenAPI specification, and the Python SDK is generated from the merged specification, so it changes when the APIs change.
<gateway>/api/<service>/v1/<subscription>/...<service>is the gateway prefix:model(chat),kb,web-crawler,agent,tools,prompt,application,user,permissions,deployment.- The gateway strips
/api/<service>, so the service sees/v1/<subscription>/.... <subscription>is the subscription slug,defaulton a new install. See Subscriptions and tenants.- Locally, the gateway is
http://localhost:8090.
Authentication
Section titled “Authentication”Send an OpenID Connect access token from your identity provider as a bearer token:
Authorization: Bearer <access token>The caller must be a platform user and a member of the subscription in the path.
Python SDK
Section titled “Python SDK”The SDK covers chat completion with streaming, models, conversations, embeddings, images, agents, applications, tools, MCP servers, prompts, knowledge bases and users. It has sync and asyncio clients and pydantic v2 models, and needs Python 3.11 or later.
from lmfoundry import LMFoundry
client = LMFoundry(base_url="http://localhost:8090", token="<access token>")
reply = client.chat.create(
model_name="gemma-4-e4b",
messages=[{"type": "user", "content": "Say hi"}],
)
print(reply.chat_response.result.text)
for chunk in client.chat.stream(model_name="gemma-4-e4b", messages=[{"type": "user", "content": "Hi"}]):
print(chunk.chat_response.result.text, end="")Connection settings
Section titled “Connection settings”| Argument | Default | Notes |
|---|---|---|
base_url | The gateway URL. Env var LMFOUNDRY_BASE_URL. Requests go to <base_url>/api/<service>/v1/<subscription>/.... | |
token | none | A fixed bearer token. Env var LMFOUNDRY_TOKEN. |
token_provider | none | A function that returns a token, called before every request. Use it when tokens expire. |
subscription | default | Filled into every path; pass subscription=... to one call to override it. |
timeout | 300 s | |
max_retries | 2 | Retries connection failures only, so a request the server may have seen is never repeated. |
Resources and errors
Section titled “Resources and errors”Operations are named after what they do: agents.list(), agents.get(id), tools.get_by_name(name), prompts.search(name=...), knowledge_bases.list_items(kb_id). iter_all() follows the pages for you.
Every failure is an lmfoundry.LMFoundryError: APIStatusError for HTTP errors (with subclasses such as AuthenticationError for 401, PermissionDeniedError for 403 and NotFoundError for 404), and APIConnectionError or APITimeoutError when no response arrived.
Contract rules
Section titled “Contract rules”- A change to a controller’s request or response regenerates the SDK.
- Chat completion streams its answer; the final chunk of an agent reply carries the grounding outcome and citations. See Agents and grounding.
- A chat
conversationIdmust be a UUID.