MacPaw / OpenAI

Swift community driven package for OpenAI public API
MIT License
2.1k stars 351 forks source link

APIError not decoded correctly #94

Closed fabstu closed 1 year ago

fabstu commented 1 year ago

Describe the bug An APIError associated with mishandling ChatGPT->Functions endpoint is not decoded correctly:

 Failed to decode request: Optional("{\n  \"error\": {\n    \"message\": [\n      \"Invalid schema for function \'hello\': In context=(\'properties\', \'a\'), array schema missing items\"\n    ],\n    \"type\": \"invalid_request_error\",\n    \"param\": null,\n    \"code\": null\n  }\n}\n")

To Reproduce

    let query = ChatQuery(
      model: .gpt3_5Turbo,
      messages: messages,
      functions: [
        .init(
          name: "hello",
          description: "Hi",
          parameters:
            JSONSchema(
              type: .object,
              properties: [
                "a": .init(
                  type: .array,
                  description: "A.",
                  enumValues: [
                    "X",
                    "Y"
                  ]
                ),
                "b": .init(
                  type: .string,
                  description: "B.",
                  enumValues: [
                    "T",
                  ]
                ),
              ],
              required: ["b"]
            )

        )
      ]
    )

    Self.logger.log("\n## Running request")

    let queryResponse = try await openAI.chats(query: query)

Expected behavior Decode error correctly.

Additional context I assume the issue is that the error message here is an array, while APIErrorResponse.message is of type String and does not handle decoding [String].

fabstu commented 1 year ago

See https://github.com/MacPaw/OpenAI/pull/95 for a fix.