# OpenAI SDKs

Source: https://convergingthought.com/docs/integrations/openai

> Connect Python or TypeScript with the Chat Completions API.



Use the standard OpenAI client on your server. Select `glm-5.3` and set the Internalize base URL, including `/v1`. An inference-only key is sufficient for these examples and for reading jobs it submitted.

## Python [#python]

```bash
pip install openai
```

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://convergingthought.com/v1",
    api_key=os.environ["INTERNALIZE_API_KEY"],
    max_retries=0,
)
response = client.chat.completions.create(
    model="glm-5.3",
    messages=[{"role": "user", "content": "Explain test-time training simply."}],
    max_completion_tokens=2048,
)
print(response.choices[0].message.content)
```

For learned weights, pass `extra_body={"subject_id": "authorized-customer-42"}` to `create`. Pin a retained version with `adapter_version` in the same object. Omit the version to resolve the active adapter at admission.

## TypeScript [#typescript]

```bash
npm install openai
```

```ts
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://convergingthought.com/v1",
  apiKey: process.env.INTERNALIZE_API_KEY,
  maxRetries: 0,
});
const response = await client.chat.completions.create({
  model: "glm-5.3",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);
```

For a subject scoped to one trusted client, add `defaultHeaders: { "X-Internalize-Subject": authorizedSubject }` when constructing that client. For a shared client, pass `headers` in the per-request options. Never mutate shared default headers between concurrent customers.

## Multiple customers [#multiple-customers]

Pair `X-Internalize-Tenant: authorizedCustomerId` with `X-Internalize-Subject: memoryName`. In TypeScript, pass both in per-request `headers`; in Python use `extra_headers`. Body `tenant_id` and header scope must agree. Repeat the tenant on job reads and learning calls. See [Serve multiple customers](/docs/guides/multitenancy) for a complete flow and restricted keys.

## Function calls [#function-calls]

Declare function tools with `name`, `description`, and `parameters`. An assistant response can contain `tool_calls`. Execute authorized functions in your runtime and append one `role: "tool"` result for each call ID before continuing. Preserve the assistant's calls in history. The next request may end with the completed tool-result block.

`tool_choice` supports `auto`, `none`, `required`, or a named function. The service validates the resulting choice before delivering it. It does not execute your tools. Strict constrained tool schemas are not supported; omit `strict` or set it to false, and validate arguments in your own handler.

## Stream responses [#stream-responses]

Set `stream: true` and optionally `stream_options: { include_usage: true }`. Consume the result with the SDK's usual stream iterator. Internalize emits keepalive comments while the durable job runs, then validated content or function-call chunks and `[DONE]`.

This is buffered SSE, not live token generation. Raw reasoning is not returned. `completion_tokens` includes the reasoning that the model generated and billed, even though only the final answer is visible.

## Errors and retries [#errors-and-retries]

The SDK exposes normal HTTP errors for admission failures such as invalid keys, insufficient funds, and invalid input. A non-streaming post-admission failure or timeout returns `409` with `error.job_id` and `error.status_url`. A stream already started returns an error object in its data frames with those same recovery fields.

Keep `maxRetries: 0` / `max_retries=0` until your application persists operation identities. For a deliberate retry, supply the same `Idempotency-Key` and exact body. A new key means a new potential charge. See [Chat Completions](/docs/api-reference/chat-completions) and the [official request reference](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create).
