async.md
Async client
Use AsyncTypeSafeClient to ask questions, list models, and configure asynchronous TypeSafe API requests.
export function SdkSignature({children}) {
async function copy(event) {
const button = event.currentTarget;
const code = button.parentElement.querySelector("pre code");
try {
await navigator.clipboard.writeText(code.textContent);
button.setAttribute("aria-label", "Signature copied");
button.dataset.copied = "true";
} catch {
button.setAttribute("aria-label", "Copy failed; select the signature to copy");
}
setTimeout(() => {
button.setAttribute("aria-label", "Copy signature");
delete button.dataset.copied;
}, 2000);
}
return <div className="sdk-signature not-prose">
<button type="button" className="sdk-signature-copy" aria-label="Copy signature" onClick={copy}>
<svg aria-hidden="true" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
<rect x="8" y="8" width="12" height="12" rx="2" />
<path d="M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3" />
</svg>
</button>
<pre tabIndex={0} aria-label="SDK signature"><code>{children}</code></pre>
</div>;
}
typesafe_sdk.AsyncTypeSafeClient
Create an asynchronous HTTP client for TypeSafe AI API.
Explicit options take precedence over environment variables; empty or whitespace-only environment values are ignored.
Logging setup
The SDK logs to the typesafe_sdk logger; configure it through standard logging, or set TYPESAFE_LOG_LEVEL (debug, info, ...) for a quick default. Secret headers are redacted from log output; request and response bodies are not.
Parameters:
api_key(str | None, default:None) – Required API key; may be set via theTYPESAFE_API_KEYenvironment variable.model(str | None, default:None) – Model name; may be set via theTYPESAFE_DEFAULT_MODELenvironment variable.retry(RetryPolicy | None, default:None) – ARetryPolicycontrolling retry behavior.timeout(float | httpx2.Timeout | None, default:None) – Timeout for HTTP operations.headers(Mapping[str, str] | None, default:None) – Additional request headers to set.transport(httpx2.AsyncBaseTransport | None, default:None) – Optional custom HTTP transport.http_client(httpx2.AsyncClient | None, default:None) – Optionalhttpx2.AsyncClient.base_url(str | None, default:None) – API root; may be set via theTYPESAFE_BASE_URLenvironment variable.
Raises:
TypeSafeError– The API key is missing or the timeout is invalid.ValueError– Bothtransportandhttp_clientare supplied.
Examples:
def main() -> None:
async with AsyncTypeSafeClient() as client:
result = await client.system_one(
state="I was charged twice. Please help.",
questions={
"billing": Noul(instructions="Is this about billing?"),
"tone": Choice(
instructions="What is the tone?",
criteria={"calm": None, "angry": None},
),
},
)
assert 0 <= result.nouls["billing"].noul <= 1
assert result.choices["tone"].choice in {"calm", "angry"}
models
cached property
typesafe_sdk.AsyncTypeSafeClient.models
An accessor for the Models API resource.
Examples:
def main() -> None:
async with AsyncTypeSafeClient() as client:
models = await client.models.list()
system_one
async
Parameters:
state(JSONContent) – Text, a JSON object, or an array to evaluate.questions(Mapping[str, Question]) – Nonempty mapping of names to question objects or raw dictionaries.model(str | None, default:None) – Model override.retry(RetryPolicy | None, default:None) – Optional retry policy to override the client-level value.timeout(float | httpx2.Timeout | None, default:None) – An optional timeout for HTTP operations.extra_headers(Mapping[str, str] | None, default:None) – Additional request headers to set.extra_body(Mapping[str, JSONValue] | None, default:None) – Additional top-level request-body fields.response_model(type[ResponseT] | None, default:None) – Optional PydanticBaseModeltype describing the JSON response body.
Returns:
SystemOneResponse | ResponseT– An instance ofresponse_model, orSystemOneResponsewith answers keyed by question.
Raises:
TypeSafeError– Questions are empty.TypeSafeAPIError– Unsuccessful HTTP response after retries.TypeSafeAPIConnectionError– The request cannot connect or times out after retries.TypeSafeAPIResponseValidationError– The response body does not match the response model.
Examples: Create questions with named arguments:
def main() -> None:
async with AsyncTypeSafeClient() as client:
result = await client.system_one(...)
aclose
async
aclose() -> None
Release network resources and close the underlying HTTP client.