Error 400 related to size of string

openai.BadRequestError: Error code: 400 - {‘error’: {‘message’: “Invalid ‘tools[0].function.name’: string too long. Expected a string with maximum length 64, but got a string with length 102 instead.”, ‘type’: ‘invalid_request_error’, ‘param’: ‘tools[0].function.name’, ‘code’: ‘string_above_max_length’}}

Any suggestions on how this could be resolved?

Carefully read the error message. It tells you which parameter, which list element in the parameter, and which property is causing the error message it shows you.

I would look in the list of tools you are sending to the API, find the first tool in the list, look at the first name parameter, and see if it is 102 letters in length, which is longer than allowed.

It is possible that you didn’t properly use matching closing quotes or some other reason why the parsing sees a long string as the function name.

The error you’re encountering (tools[0].function.name: string too long. Expected a string with maximum length 64) occurs because the operationId in your OpenAPI specification exceeds 64 characters.

The operationId is automatically derived from the name in the OpenAPI spec and must be 64 characters or fewer.

For example, the following operationId is too long:

"operationId": "create_assistant_with_tools_endpoint_api_v2_assistant_create_with_tools_post"

Here’s the relevant example of the OpenAPI specification:

"/api/v2/assistant/create_with_tools": {
  "post": {
    "tags": [
      "Assistants V2"
    ],
    "summary": "Create Assistant With Tools Endpoint",
    "operationId": "create_assistant_with_tools_endpoint_api_v2_assistant_create_with_tools_post",
    "parameters": [
      {
        "name": "openai_api_key",
        "in": "query",
        "required": false,
        "schema": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "description": "Optional OpenAI API key",
          "title": "Openai Api Key"
        },
        "description": "Optional OpenAI API key"
      }
    ],
    "requestBody": {
      "required": true,
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/CreateAssistantWithToolsRequest"
          }
        }
      }
    },
    "responses": {
      "200": {
        "description": "Successful Response",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Assistant"
            }
          }
        }
      },
      "422": {
        "description": "Validation Error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/HTTPValidationError"
            }
          }
        }
      }
    }
  }
}

To resolve this, if you’re using FastAPI to define your API routers, you can set a shorter operation_id in the route definition like this:

@router_assistant.post("/create_assistant", operation_id="create_assistant")

This will keep the operationId within the 64-character limit, eliminating the error.