fix: handle non-picklable original_exception in MidStreamFallbackError

The __getstate__ mixin was storing attributes as-is into the pickle
state dict. For openai SDK exceptions (e.g. openai.RateLimitError),
pickle.dumps succeeds but pickle.loads fails because the openai
__init__ requires response and body keyword arguments that the
default pickle reconstruction does not supply.

Fix: probe each non-httpx attribute with a full round-trip
(pickle.loads(pickle.dumps(v))) before storing it. If the round-trip
fails, store str(v) instead so the attribute is still meaningful
after unpickling.

Covers MidStreamFallbackError.original_exception and any other
attribute that is dumps-able but not loads-able.
This commit is contained in:
Jason Matthew Suhari 2026-03-21 15:53:36 +08:00
parent f8b0615ae9
commit 1113598c57

View file

@ -9,6 +9,7 @@
## LiteLLM versions of the OpenAI Exception Types
import pickle
from typing import Optional
import httpx
@ -87,7 +88,11 @@ class _LiteLLMPickleMixin:
elif isinstance(v, httpx.Request):
http_attrs.append(k)
else:
state[k] = v
try:
pickle.loads(pickle.dumps(v))
state[k] = v
except Exception:
state[k] = str(v)
if http_attrs:
state["_pickled_http_attrs"] = http_attrs
return state