Completions

Options and Validation

Configure direct completion requests and understand validation behavior.

createCompletion and createCompletionStream use the same option shape.

type CreateCompletionBaseOptions = {
  input?: string | Message | Message[];
  messages?: Message[];
  instructions?: string;
  documents?: Document[];
  tools?: ToolDefinition[];
  temperature?: number;
  maxTokens?: number;
  toolChoice?: ToolChoice;
  outputSchema?: JsonObject;
  params?: JsonValue;
};

createParsedCompletion uses the same shape, but replaces outputSchema with schema:

type CreateParsedCompletionOptions<T> =
  Omit<CreateCompletionBaseOptions, "outputSchema"> & {
    schema: ZodSchema<T>;
  };

Provider Params

Use params for provider-specific request options. Anvia forwards it to CompletionRequest.additionalParams.

const result = await createCompletion(model, {
  input: "Classify this ticket: I cannot reset my password.",
  params: {
    reasoning: { effort: "low" },
  },
});

Keep params provider-aware. A value accepted by one provider can be ignored or rejected by another provider.

Tool Definitions

Direct completions can send tool definitions to the model, but they do not execute tools. Tool calls are returned as assistant content or stream events for your application to handle.

const result = await createCompletion(model, {
  input: "Check order A123.",
  tools: [
    {
      name: "lookup_order",
      description: "Look up an order by id.",
      parameters: {
        type: "object",
        properties: {
          orderId: { type: "string" },
        },
        required: ["orderId"],
      },
    },
  ],
  toolChoice: "auto",
});

Use an agent when Anvia should execute tools and continue the model loop.

Capability Validation

Both helpers validate request features against model.capabilities before calling the provider. Unsupported tools, tool choice, image input, file document input, output schemas, and streaming fail before the provider request is made.

createCompletion rejects through the returned promise:

try {
  await createCompletion(model, {
    input: "Use this screenshot.",
    tools: [
      {
        name: "lookup_order",
        description: "Look up an order by id.",
        parameters: { type: "object" },
      },
    ],
  });
} catch (error) {
  console.error(error);
}

createCompletionStream throws before returning the stream when validation fails.