TypeScript SDK: Chat completions

⚠️
Preview — this feature is being built; the design below is the spec.

Methods for POST /v1/chat/completions and its helpers.

import Syntic from '@syntic/sdk'
 
const client = new Syntic() // reads SYNTIC_API_KEY
MethodEndpointReturns
chat.completions.createPOST /v1/chat/completionsChatCompletion
chat.completions.streamPOST /v1/chat/completionsChatCompletionStream (context manager / async iterable)
chat.completions.parsePOST /v1/chat/completionsParsedChatCompletion[T]
chat.completions.countTokensPOST /v1/chat/completions/count_tokensTokenCount with input_tokens, remaining_tokens

Methods

chat.completions.create

Send a conversation and get the reply. Pass stream=True to get an iterator of chunks instead. Maps to POST /v1/chat/completions.

const result = await client.chat.completions.create({
  model: "Amara S1.2",
  messages: [
    {
      role: "user",
      content: "Hello"
    }
  ],
  max_tokens: 300
})

chat.completions.stream

Stream with a helper that accumulates text, tool calls and usage, and exposes the final message when done. Maps to POST /v1/chat/completions.

for await (const item of client.chat.completions.stream({
  model: "Amara S1.2",
  messages: [
    {
      role: "user",
      content: "Tell me a story"
    }
  ]
})) {
  console.log(item)
}

chat.completions.parse

Structured output from a Pydantic model (Python) or Zod schema (TypeScript); returns the parsed object on message.parsed. Maps to POST /v1/chat/completions.

const result = await client.chat.completions.parse({
  model: "Amara Quick",
  messages: [
    {
      role: "user",
      content: "…"
    }
  ],
  response_format: zodResponseFormat(Ticket, 'ticket')
})

chat.completions.countTokens

Count input tokens for a request body without running it. Maps to POST /v1/chat/completions/count_tokens.

const result = await client.chat.completions.countTokens({
  model: "Amara S1.2",
  messages: [
    {
      role: "user",
      content: "Hello"
    }
  ]
})

Conventions

Every method accepts a final options object with headers, query, timeout, maxRetries and signal (an AbortSignal). List methods return pages that work with for await across all pages.

See the TypeScript SDK overview for installation, errors and retries.

Was this page helpful?