Playground gives unusable code examples for typescript api

I’m using the chat playground to test and debug my prompts and values to use but when I copy the nodejs code example from the playground and use it in my typescript application I get the following errors that prevent the application from starting

No overload matches this call.
  Overload 3 of 3, '(body: ChatCompletionCreateParamsBase, options?: RequestOptions<unknown> | undefined): APIPromise<ChatCompletion | Stream<...>>', gave the following error.
    Type '{ type: string; text: string; }[]' is not assignable to type 'string | ChatCompletionContentPart[] | null | undefined'.
      Type '{ type: string; text: string; }' is not assignable to type 'ChatCompletionContentPart'.
        Types of property 'type' are incompatible.
          Type 'string' is not assignable to type '"text" | "image_url"'.
  Overload 3 of 3, '(body: ChatCompletionCreateParamsBase, options?: RequestOptions<unknown> | undefined): APIPromise<ChatCompletion | Stream<...>>', gave the following error.
    Type '{ type: string; text: string; }[]' is not assignable to type 'string | ChatCompletionContentPart[] | null | undefined'.
      Type '{ type: string; text: string; }' is not assignable to type 'ChatCompletionContentPart'.
        Types of property 'type' are incompatible.
          Type 'string' is not assignable to type '"text" | "image_url"'.
  Overload 3 of 3, '(body: ChatCompletionCreateParamsBase, options?: RequestOptions<unknown> | undefined): APIPromise<ChatCompletion | Stream<...>>', gave the following error.
    Type '{ type: string; text: string; }[]' is not assignable to type 'string | ChatCompletionContentPart[] | null | undefined'.
      Type '{ type: string; text: string; }' is not assignable to type 'ChatCompletionContentPart'.
        Types of property 'type' are incompatible.
          Type 'string' is not assignable to type '"text" | "image_url"'.ts(2769)
completions.d.ts(449, 5): The expected type comes from property 'content' which is declared here on type 'ChatCompletionMessageParam'
completions.d.ts(449, 5): The expected type comes from property 'content' which is declared here on type 'ChatCompletionMessageParam'
completions.d.ts(449, 5): The expected type comes from property 'content' which is declared here on type 'ChatCompletionMessageParam'

After checking the type of each property one by one I noticed that this is because if the role of the message is “system” the type required for the content property is strictly a string

If the role is “assistant” the type for content is also required to be a string, null or undefined but not an array of objects as the ones provided by the playground

The arrays provided in playground are only accepted as a content value for the “user” role

This doesn’t affect regular JS application as they don’t require types but it’s very annoying to copy the exact example given and not being able to use it out of the box in typescript

I’m using the latest version of the api “openai”: “^4.47.1”

1 Like

The openai-node package validation blocks such requests:

export interface ChatCompletionSystemMessageParam {
/**

  • The contents of the system message.
    */
    content: string;

/**

  • The role of the messages author, in this case system.
    */
    role: ‘system’;

/**

  • An optional name for the participant. Provides the model information to
  • differentiate between participants of the same role.
    */
    name?: string;
    }

Better to just avoid constantly inferior and instantly out-of-date request-blocking code, and write your own request, which is successful.

import os, httpx, json

apikey = os.environ.get("OPENAI_API_KEY")
url = "https://api.openai.com/v1/chat/completions"
headers = {
    "OpenAI-Beta": "assistants=v2",
    "Authorization": f"Bearer {apikey}"
}
body = {
    "model": "gpt-4o-2024-05-13", "max_tokens": 25, "top_p": 0.8,
    "messages": [
        {"role": "system",
         "content": [{"type": "text", "text": "Hello robot"}]
        }
    ]}

try:
    response = httpx.post(url, headers=headers, json=body)
    response = response.json()
    print(json.dumps(response, indent=3))
except Exception as e:
    print(e)
    raise