openai-compatible-error.ts 893 B

123456789101112131415161718192021222324252627
  1. import { z, type ZodType } from "zod/v4"
  2. export const openaiCompatibleErrorDataSchema = z.object({
  3. error: z.object({
  4. message: z.string(),
  5. // The additional information below is handled loosely to support
  6. // OpenAI-compatible providers that have slightly different error
  7. // responses:
  8. type: z.string().nullish(),
  9. param: z.any().nullish(),
  10. code: z.union([z.string(), z.number()]).nullish(),
  11. }),
  12. })
  13. export type OpenAICompatibleErrorData = z.infer<typeof openaiCompatibleErrorDataSchema>
  14. export type ProviderErrorStructure<T> = {
  15. errorSchema: ZodType<T>
  16. errorToMessage: (error: T) => string
  17. isRetryable?: (response: Response, error?: T) => boolean
  18. }
  19. export const defaultOpenAICompatibleErrorStructure: ProviderErrorStructure<OpenAICompatibleErrorData> = {
  20. errorSchema: openaiCompatibleErrorDataSchema,
  21. errorToMessage: (data) => data.error.message,
  22. }