Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Sources/OpenAI/Public/ResponseModels/OpenAIErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,26 @@ public struct OpenAIErrorResponse: Decodable {
public let type: String?
public let param: String?
public let code: String?

enum CodingKeys: String, CodingKey {
case message, type, param, code
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
message = try container.decodeIfPresent(String.self, forKey: .message)
type = try container.decodeIfPresent(String.self, forKey: .type)
param = try container.decodeIfPresent(String.self, forKey: .param)

// Some OpenAI-compatible providers (e.g. OpenRouter) provide literal response status codes in the "code" field (e.g., 403)
// Try decoding "code" first as an Int, then fallback to a String
if let intCode = try? container.decodeIfPresent(Int.self, forKey: .code) {
code = String(intCode)
} else if let stringCode = try? container.decodeIfPresent(String.self, forKey: .code) {
code = stringCode
} else {
code = nil
}
}
}
}
}