> ## Documentation Index
> Fetch the complete documentation index at: https://developers.telnyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a chat completion (OpenAI-compatible)

> Chat with a language model. This endpoint is consistent with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and may be used with the OpenAI JS or Python SDK by setting the base URL to `https://api.telnyx.com/v2/ai/openai`.



## OpenAPI

````yaml https://telnyx-openapi-ng.s3.us-east-1.amazonaws.com/ai/inference.yml post /ai/openai/chat/completions
openapi: 3.1.0
info:
  title: Telnyx AI Inference API
  version: 2.0.0
  description: API for AI inference, including chat completions, fine-tuning, and models.
  contact:
    email: support@telnyx.com
servers:
  - url: https://api.telnyx.com/v2
security:
  - bearerAuth: []
paths:
  /ai/openai/chat/completions:
    post:
      tags:
        - OpenAI Chat
      summary: Create a chat completion (OpenAI-compatible)
      description: >-
        Chat with a language model. This endpoint is consistent with the [OpenAI
        Chat Completions
        API](https://platform.openai.com/docs/api-reference/chat) and may be
        used with the OpenAI JS or Python SDK by setting the base URL to
        `https://api.telnyx.com/v2/ai/openai`.
      operationId: create_openai_chat_completion
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
            example:
              messages:
                - role: system
                  content: You are a friendly chatbot.
                - role: user
                  content: Hello, world!
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                type: object
                additionalProperties: true
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import Telnyx from 'telnyx';

            const client = new Telnyx({
              apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
            });

            const response = await client.ai.openai.chat.createCompletion({
              messages: [
                { role: 'system', content: 'You are a friendly chatbot.' },
                { role: 'user', content: 'Hello, world!' },
              ],
            });

            console.log(response);
        - lang: Python
          source: |-
            import os
            from telnyx import Telnyx

            client = Telnyx(
                api_key=os.environ.get("TELNYX_API_KEY"),  # This is the default and can be omitted
            )
            response = client.ai.openai.chat.create_completion(
                messages=[{
                    "role": "system",
                    "content": "You are a friendly chatbot.",
                }, {
                    "role": "user",
                    "content": "Hello, world!",
                }],
            )
            print(response)
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/team-telnyx/telnyx-go\"\n\t\"github.com/team-telnyx/telnyx-go/option\"\n)\n\nfunc main() {\n\tclient := telnyx.NewClient(\n\t\toption.WithAPIKey(\"My API Key\"),\n\t)\n\tresponse, err := client.AI.OpenAI.Chat.NewCompletion(context.TODO(), telnyx.AIOpenAIChatNewCompletionParams{\n\t\tMessages: []telnyx.AIOpenAIChatNewCompletionParamsMessage{{\n\t\t\tRole: \"system\",\n\t\t\tContent: telnyx.AIOpenAIChatNewCompletionParamsMessageContentUnion{\n\t\t\t\tOfString: telnyx.String(\"You are a friendly chatbot.\"),\n\t\t\t},\n\t\t}, {\n\t\t\tRole: \"user\",\n\t\t\tContent: telnyx.AIOpenAIChatNewCompletionParamsMessageContentUnion{\n\t\t\t\tOfString: telnyx.String(\"Hello, world!\"),\n\t\t\t},\n\t\t}},\n\t})\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfmt.Printf(\"%+v\\n\", response)\n}\n"
        - lang: Java
          source: >-
            package com.telnyx.sdk.example;


            import com.telnyx.sdk.client.TelnyxClient;

            import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;

            import
            com.telnyx.sdk.models.ai.openai.chat.ChatCreateCompletionParams;

            import
            com.telnyx.sdk.models.ai.openai.chat.ChatCreateCompletionResponse;


            public final class Main {
                private Main() {}

                public static void main(String[] args) {
                    TelnyxClient client = TelnyxOkHttpClient.fromEnv();

                    ChatCreateCompletionParams params = ChatCreateCompletionParams.builder()
                        .addMessage(ChatCreateCompletionParams.Message.builder()
                            .content("You are a friendly chatbot.")
                            .role(ChatCreateCompletionParams.Message.Role.SYSTEM)
                            .build())
                        .addMessage(ChatCreateCompletionParams.Message.builder()
                            .content("Hello, world!")
                            .role(ChatCreateCompletionParams.Message.Role.USER)
                            .build())
                        .build();
                    ChatCreateCompletionResponse response = client.ai().openai().chat().createCompletion(params);
                }
            }
        - lang: Ruby
          source: |-
            require "telnyx"

            telnyx = Telnyx::Client.new(api_key: "My API Key")

            response = telnyx.ai.openai.chat.create_completion(
              messages: [{content: "You are a friendly chatbot.", role: :system}, {content: "Hello, world!", role: :user}]
            )

            puts(response)
        - lang: PHP
          source: >-
            <?php


            require_once dirname(__DIR__) . '/vendor/autoload.php';


            use Telnyx\Client;

            use Telnyx\Core\Exceptions\APIException;


            $client = new Client(apiKey: getenv('TELNYX_API_KEY') ?: 'My API
            Key');


            try {
              $response = $client->ai->openai->chat->createCompletion(
                messages: [
                  ['content' => 'You are a friendly chatbot.', 'role' => 'system'],
                  ['content' => 'Hello, world!', 'role' => 'user'],
                ],
                apiKeyRef: 'api_key_ref',
                bestOf: 0,
                earlyStopping: true,
                enableThinking: true,
                frequencyPenalty: 0,
                guidedChoice: ['string'],
                guidedJson: ['foo' => 'bar'],
                guidedRegex: 'guided_regex',
                lengthPenalty: 0,
                logprobs: true,
                maxTokens: 0,
                minP: 0,
                model: 'model',
                n: 0,
                presencePenalty: 0,
                responseFormat: ['type' => 'text'],
                seed: 0,
                stop: 'string',
                stream: true,
                temperature: 0,
                toolChoice: 'none',
                tools: [
                  [
                    'function' => [
                      'name' => 'name',
                      'description' => 'description',
                      'parameters' => ['foo' => 'bar'],
                    ],
                    'type' => 'function',
                  ],
                ],
                topLogprobs: 0,
                topP: 0,
                useBeamSearch: true,
              );

              var_dump($response);
            } catch (APIException $e) {
              echo $e->getMessage();
            }
        - lang: CLI
          source: |-
            telnyx ai:openai:chat create-completion \
              --api-key 'My API Key' \
              --message '{content: You are a friendly chatbot., role: system}' \
              --message "{content: 'Hello, world!', role: user}"
components:
  schemas:
    ChatCompletionRequest:
      properties:
        messages:
          items:
            $ref: '#/components/schemas/ChatCompletionSystemMessageParam'
          type: array
          example:
            - role: system
              content: You are a friendly chatbot.
            - role: user
              content: Hello, world!
          description: A list of the previous chat messages for context.
        model:
          type: string
          default: meta-llama/Meta-Llama-3.1-8B-Instruct
          description: The language model to chat with.
        api_key_ref:
          type: string
          description: >-
            If you are using an external inference provider like xAI or OpenAI,
            this field allows you to pass along a reference to your API key.
            After creating an [integration
            secret](https://developers.telnyx.com/api-reference/integration-secrets/create-a-secret)
            for you API key, pass the secret's `identifier` in this field.
        stream:
          type: boolean
          default: false
          description: >-
            Whether or not to stream data-only server-sent events as they become
            available.
        temperature:
          type: number
          default: 0.1
          description: >-
            Adjusts the "creativity" of the model. Lower values make the model
            more deterministic and repetitive, while higher values make the
            model more random and creative.
        max_tokens:
          type: integer
          description: Maximum number of completion tokens the model should generate.
        tools:
          items:
            oneOf:
              - $ref: '#/components/schemas/ChatCompletionToolParam'
              - $ref: '#/components/schemas/Retrieval'
          type: array
          description: >-
            The `function` tool type follows the same schema as the [OpenAI Chat
            Completions
            API](https://platform.openai.com/docs/api-reference/chat). The
            `retrieval` tool type is unique to Telnyx. You may pass a list of
            [embedded storage
            buckets](https://developers.telnyx.com/api-reference/embeddings/embed-documents)
            for retrieval-augmented generation.
        tool_choice:
          type: string
          enum:
            - none
            - auto
            - required
        response_format:
          $ref: '#/components/schemas/ChatCompletionResponseFormatParam'
          description: >-
            Use this is you want to guarantee a JSON output without defining a
            schema. For control over the schema, use `guided_json`.
        guided_json:
          type: object
          description: >-
            Must be a valid JSON schema. If specified, the output will follow
            the JSON schema.
          additionalProperties: true
        guided_regex:
          type: string
          description: If specified, the output will follow the regex pattern.
        guided_choice:
          type: array
          items:
            type: string
          description: If specified, the output will be exactly one of the choices.
        min_p:
          type: number
          description: >-
            This is an alternative to `top_p` that [many
            prefer](https://github.com/huggingface/transformers/issues/27670).
            Must be in [0, 1].
        'n':
          type: number
          description: >-
            This will return multiple choices for you instead of a single chat
            completion.
        use_beam_search:
          type: boolean
          default: false
          description: >-
            Setting this to `true` will allow the model to [explore more
            completion
            options](https://huggingface.co/blog/how-to-generate#beam-search).
            This is not supported by OpenAI.
        best_of:
          type: integer
          description: >-
            This is used with `use_beam_search` to determine how many candidate
            beams to explore.
        length_penalty:
          type: number
          default: 1
          description: >-
            This is used with `use_beam_search` to prefer shorter or longer
            completions.
        early_stopping:
          type: boolean
          default: false
          description: >-
            This is used with `use_beam_search`. If `true`, generation stops as
            soon as there are `best_of` complete candidates; if `false`, a
            heuristic is applied and the generation stops when is it very
            unlikely to find better candidates.
        logprobs:
          type: boolean
          default: false
          description: >-
            Whether to return log probabilities of the output tokens or not. If
            true, returns the log probabilities of each output token returned in
            the `content` of `message`.
        top_logprobs:
          type: integer
          description: >-
            This is used with `logprobs`. An integer between 0 and 20 specifying
            the number of most likely tokens to return at each token position,
            each with an associated log probability.
        frequency_penalty:
          type: number
          default: 0
          description: >-
            Higher values will penalize the model from repeating the same output
            tokens.
        presence_penalty:
          type: number
          default: 0
          description: >-
            Higher values will penalize the model from repeating the same output
            tokens.
        top_p:
          type: number
          description: >-
            An alternative or complement to `temperature`. This adjusts how many
            of the top possibilities to consider.
        stop:
          description: >-
            Up to 4 sequences where the API will stop generating further tokens.
            The returned text will not contain the stop sequence.
          oneOf:
            - type: string
            - type: array
              items:
                type: string
              maxItems: 4
        seed:
          type: integer
          description: >-
            If specified, the system will make a best effort to sample
            deterministically, such that repeated requests with the same `seed`
            and parameters should return the same result.
        enable_thinking:
          type: boolean
          default: true
          description: >-
            Whether to enable the thinking/reasoning phase for models that
            support it (e.g., QwQ, Qwen3). When set to false, the model will
            skip the internal reasoning step and respond directly, which can
            reduce latency. Defaults to true.
      type: object
      required:
        - messages
      title: ChatCompletionRequest
    HTTPValidationError:
      title: HTTPValidationError
      type: object
      properties:
        detail:
          title: Detail
          type: array
          items:
            $ref: '#/components/schemas/ValidationError'
    ChatCompletionSystemMessageParam:
      type: object
      properties:
        content:
          oneOf:
            - title: Content String
              type: string
            - title: Text and Image Array
              type: array
              items:
                type: object
                properties:
                  type:
                    type: string
                    enum:
                      - text
                      - image_url
                  text:
                    type: string
                  image_url:
                    type: string
                required:
                  - type
                additionalProperties: false
        role:
          type: string
          enum:
            - system
            - user
            - assistant
            - tool
      required:
        - content
        - role
      title: ChatCompletionSystemMessageParam
    ChatCompletionToolParam:
      properties:
        type:
          type: string
          enum:
            - function
        function:
          $ref: '#/components/schemas/FunctionDefinition'
      type: object
      required:
        - function
        - type
      title: Function
    Retrieval:
      properties:
        type:
          type: string
          enum:
            - retrieval
        retrieval:
          $ref: '#/components/schemas/BucketIds'
      type: object
      required:
        - type
        - retrieval
      title: Retrieval
    ChatCompletionResponseFormatParam:
      properties:
        type:
          type: string
          enum:
            - text
            - json_object
      type: object
      required:
        - type
      title: ChatCompletionResponseFormatParam
    ValidationError:
      title: ValidationError
      required:
        - loc
        - msg
        - type
      type: object
      properties:
        loc:
          title: Location
          type: array
          items:
            anyOf:
              - type: string
              - type: integer
        msg:
          title: Message
          type: string
        type:
          title: Error Type
          type: string
    FunctionDefinition:
      properties:
        name:
          type: string
        description:
          type: string
        parameters:
          type: object
          additionalProperties: true
      type: object
      required:
        - name
      title: FunctionDefinition
    BucketIds:
      properties:
        bucket_ids:
          items:
            type: string
          type: array
          description: >-
            List of [embedded storage
            buckets](https://developers.telnyx.com/api-reference/embeddings/embed-documents)
            to use for retrieval-augmented generation.
        max_num_results:
          description: >-
            The maximum number of results to retrieve as context for the
            language model.
          type: integer
      type: object
      required:
        - bucket_ids
      title: BucketIds
  securitySchemes:
    bearerAuth:
      scheme: bearer
      type: http

````