Can messages have metadata in chat completions?

can messages have metadata in https://api.openai.com/v1/chat/completions?

$messages = [];

$messages[] = [
                    "role" =>  "user" ,
                    "content" => 'Sample text',
                    "metadata" => [
                               "user-real-name" => "Akbari"
                    ]
                ];

$response = Http::withHeaders([
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . $apiKey,
        ])->post('https://api.openai.com/v1/chat/completions', [
            'model' => $model,
            'messages' => $messages,
            'max_tokens' => $maxTokens,
            'temperature' => $temperature,
        ]);

Hi @mohamadaliakbari ! It’s only role and content that is accepted. You can define metadata in your system prompt and hope for the best (generally the model will take your metadata into account).

1 Like

@platypus I read in the chat compleitions documentation that we can add a ‘metadata’ parameter to keep additional information, but got this error “TypeError: got an unexpected keyword argument ‘metadata’”

Here is my attempt:

      response = client.chat.completions.create(
          model=model_version,
          messages=messages,
          max_tokens=1000,
          temperature=0,
          response_format=structured_output_format,
          metadata={"row_id": df.id}
      )

Appreciate your help.

1 Like

Hi @ning.li !

Ok I figured it out - that metadata parameter is only used when you are storing your chat completions, e.g. for use with evaluation or distillation - so you have to also set store to true, in combination with metadata.

I did a simple call with "metadata": {"foo": "bar"}, and "store": true and this is what it looks like on my dashboard:

I hope that clarifies it!

In the meantime I will also create a note that the API reference should be updated, i.e. store must be set to true otherwise you will get the following error:

  "error": {
    "message": "The 'metadata' parameter is only allowed when 'store' is enabled.",
    "type": "invalid_request_error",
    "param": "metadata",
    "code": null
  }

OK! Thanks for the clarification. So if I want to add metadata to the request without storing it, it would have to be managed outside the request (e.g., appending the ID to a table that stores the response when invoking a function to make the request). Is that right?