From 1113598c57b3c8afc5b4e1d2fa05bf48b78ba6ec Mon Sep 17 00:00:00 2001 From: Jason Matthew Suhari Date: Sat, 21 Mar 2026 15:53:36 +0800 Subject: [PATCH] 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. --- litellm/exceptions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/litellm/exceptions.py b/litellm/exceptions.py index cfcd631fb24..2eabb494d0f 100644 --- a/litellm/exceptions.py +++ b/litellm/exceptions.py @@ -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