Skip to main content
POST
/
v1
/
chat
/
completions
Chat Completions
curl --request POST \
  --url https://api.example.com/v1/chat/completions
{
  "id": "<string>",
  "object": "<string>",
  "created": {},
  "model": "<string>",
  "choices": {
    "index": {},
    "message": {},
    "finish_reason": "<string>",
    "logprobs": {}
  },
  "usage": {
    "prompt_tokens": {},
    "completion_tokens": {},
    "total_tokens": {}
  },
  "tools_executed": {},
  "mcp_server_errors": {},
  "system_fingerprint": "<string>"
}

Overview

Generates a model response for the given conversation and configuration. Supports OpenAI-compatible parameters and provider-specific extensions.

Method Signature

func (r *ChatCompletionService) New(
    ctx context.Context,
    body ChatCompletionNewParams,
    opts ...option.RequestOption,
) (*ChatCompletion, error)

Streaming Method

func (r *ChatCompletionService) NewStreaming(
    ctx context.Context,
    body ChatCompletionNewParams,
    opts ...option.RequestOption,
) *ssestream.Stream[ChatCompletionChunk]

Request Parameters

messages
[]ChatCompletionMessageParam
required
Array of messages in the conversation. Each message has a role (system, user, assistant, developer, function, tool) and content.
model
string
required
ID of the model to use. Format: provider/model-name (e.g., openai/gpt-4, anthropic/claude-3-5-sonnet-20241022)
stream
bool
default:"false"
Whether to stream responses using Server-Sent Events (SSE)
temperature
float64
default:"1.0"
Sampling temperature between 0 and 2. Higher values make output more random.
max_tokens
int64
Maximum number of tokens to generate in the completion
top_p
float64
default:"1.0"
Nucleus sampling parameter. Alternative to temperature.
n
int64
default:"1"
Number of chat completion choices to generate
stop
[]string
Up to 4 sequences where the API will stop generating tokens
presence_penalty
float64
default:"0"
Penalty for new tokens based on whether they appear in the text so far (-2.0 to 2.0)
frequency_penalty
float64
default:"0"
Penalty for new tokens based on their frequency in the text so far (-2.0 to 2.0)
tools
[]ChatCompletionToolParam
List of tools the model may call. Use this for function calling.
tool_choice
ToolChoiceUnionParam
Controls which (if any) tool is called. Options: auto, none, any, or specific tool.
response_format
ResponseFormatParam
Format for the model’s output. Supports text, json_object, or json_schema.
user
string
Unique identifier for the end-user for abuse monitoring

Response Fields

id
string
required
Unique identifier for the chat completion
object
string
required
Object type, always chat.completion
created
int64
required
Unix timestamp (in seconds) when the completion was created
model
string
required
The model used for the completion
choices
[]Choice
required
Array of chat completion choices
usage
CompletionUsage
Token usage statistics
tools_executed
[]string
List of tool names executed server-side (MCP tools)
mcp_server_errors
map[string]interface{}
Information about MCP server failures, if any occurred
system_fingerprint
string
Backend configuration fingerprint for determinism tracking

Code Examples

Basic Chat Completion

package main

import (
    "context"
    "fmt"
    "log"

    dedalus "github.com/dedalus-labs/dedalus-sdk-go"
    "github.com/dedalus-labs/dedalus-sdk-go/option"
)

func main() {
    client := dedalus.NewClient(
        option.WithAPIKey("your-api-key"),
    )

    ctx := context.Background()
    
    completion, err := client.Chat.Completions.New(ctx, dedalus.ChatCompletionNewParams{
        Model: dedalus.F("openai/gpt-4"),
        Messages: dedalus.F([]dedalus.ChatCompletionMessageParamUnion{
            dedalus.ChatCompletionUserMessageParam{
                Role:    dedalus.F(dedalus.ChatCompletionUserMessageParamRoleUser),
                Content: dedalus.F(dedalus.ChatCompletionUserMessageParamContentUnion(
                    dedalus.String("What is the capital of France?"),
                )),
            },
        }),
    })

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(completion.Choices[0].Message.Content)
}

Streaming Chat Completion

stream := client.Chat.Completions.NewStreaming(ctx, dedalus.ChatCompletionNewParams{
    Model: dedalus.F("openai/gpt-4"),
    Messages: dedalus.F([]dedalus.ChatCompletionMessageParamUnion{
        dedalus.ChatCompletionUserMessageParam{
            Role:    dedalus.F(dedalus.ChatCompletionUserMessageParamRoleUser),
            Content: dedalus.F(dedalus.ChatCompletionUserMessageParamContentUnion(
                dedalus.String("Tell me a story"),
            )),
        },
    }),
})

for stream.Next() {
    chunk := stream.Current()
    if len(chunk.Choices) > 0 {
        fmt.Print(chunk.Choices[0].Delta.Content)
    }
}

if err := stream.Err(); err != nil {
    log.Fatal(err)
}

Function Calling

completion, err := client.Chat.Completions.New(ctx, dedalus.ChatCompletionNewParams{
    Model: dedalus.F("openai/gpt-4"),
    Messages: dedalus.F([]dedalus.ChatCompletionMessageParamUnion{
        dedalus.ChatCompletionUserMessageParam{
            Role:    dedalus.F(dedalus.ChatCompletionUserMessageParamRoleUser),
            Content: dedalus.F(dedalus.ChatCompletionUserMessageParamContentUnion(
                dedalus.String("What's the weather in San Francisco?"),
            )),
        },
    }),
    Tools: dedalus.F([]dedalus.ChatCompletionToolParam{
        {
            Type: dedalus.F(dedalus.ChatCompletionToolParamTypeFunction),
            Function: dedalus.F(shared.FunctionDefinitionParam{
                Name:        dedalus.F("get_weather"),
                Description: dedalus.F("Get the current weather in a location"),
                Parameters: dedalus.F(shared.FunctionParameters{
                    "type": "object",
                    "properties": map[string]interface{}{
                        "location": map[string]interface{}{
                            "type": "string",
                            "description": "The city name",
                        },
                    },
                    "required": []string{"location"},
                }),
            }),
        },
    }),
})

Message Types

The SDK supports multiple message types:
  • System Message: Instructions for the model
  • User Message: User input (supports text, images, audio, files)
  • Assistant Message: Model responses
  • Tool Message: Tool execution results
  • Function Message: Function call results (deprecated)
  • Developer Message: Developer instructions (replaces system for o1 models)

Error Responses

  • 400 Bad Request: Validation error in request parameters
  • 401 Unauthorized: Invalid or missing API key
  • 402 Payment Required: Insufficient balance or quota
  • 429 Too Many Requests: Rate limit exceeded
  • 500 Internal Server Error: Unexpected server failure

Build docs developers (and LLMs) love