Skip to content

APIError and subclasses are not unpicklable #1144

@olsaarik

Description

@olsaarik

Environment details

  • Programming language: Python
  • OS: Linux
  • Language runtime version: 3.12
  • Package version: 1.24.0

Steps to reproduce

from google.genai.errors import APIError
import pickle
pickle.loads(pickle.dumps(APIError(1, {})))

This throws TypeError: APIError.__init__() missing 1 required positional argument: 'response_json'

A workaround for my use case is instead using a subclass like:

class PicklableAPIError(APIError):
     def __reduce__(self):
         state = self.__dict__.copy()
         return (self._rebuild, (state,))
 
     @staticmethod
     def _rebuild(state):
         obj = PicklableAPIError.__new__(PicklableAPIError)
         obj.__dict__.update(state)
         Exception.__init__(obj, f'{obj.code} {obj.status}. {obj.details}')
         return obj

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions