Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

[Fix] - shared session parsing and usage issue (#15440)

Alexsander Hamir2c9356c437e2
diff --git a/litellm/llms/openai/openai.py b/litellm/llms/openai/openai.py
index 3347e533242..324205237dc 100644
--- a/litellm/llms/openai/openai.py
+++ b/litellm/llms/openai/openai.py
@@ -1125,6 +1125,7 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
api_base: Optional[str] = None,
client: Optional[AsyncOpenAI] = None,
max_retries=None,
+ shared_session: Optional["ClientSession"] = None,
):
try:
openai_aclient: AsyncOpenAI = self._get_openai_client( # type: ignore
@@ -1134,6 +1135,7 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
timeout=timeout,
max_retries=max_retries,
client=client,
+ shared_session=shared_session,
)
headers, response = await self.make_openai_embedding_request(
openai_aclient=openai_aclient,
@@ -1197,6 +1199,7 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
client=None,
aembedding=None,
max_retries: Optional[int] = None,
+ shared_session: Optional["ClientSession"] = None,
) -> EmbeddingResponse:
super().embedding()
try:
@@ -1223,6 +1226,7 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
timeout=timeout,
client=client,
max_retries=max_retries,
+ shared_session=shared_session,
)
openai_client: OpenAI = self._get_openai_client( # type: ignore
diff --git a/litellm/main.py b/litellm/main.py
index 6cdba2e8e25..3955a0f32f8 100644
--- a/litellm/main.py
+++ b/litellm/main.py
@@ -3996,6 +3996,7 @@ def embedding( # noqa: PLR0915
"""
azure = kwargs.get("azure", None)
client = kwargs.pop("client", None)
+ shared_session = kwargs.get("shared_session", None)
max_retries = kwargs.get("max_retries", None)
litellm_logging_obj: LiteLLMLoggingObj = kwargs.get("litellm_logging_obj") # type: ignore
mock_response: Optional[List[float]] = kwargs.get("mock_response", None) # type: ignore
@@ -4185,6 +4186,7 @@ def embedding( # noqa: PLR0915
client=client,
aembedding=aembedding,
max_retries=max_retries,
+ shared_session=shared_session,
)
elif custom_llm_provider == "databricks":
api_base = api_base or litellm.api_base or get_secret("DATABRICKS_API_BASE") # type: ignore
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index 34bc88ee76b..a897942ef3f 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -2382,6 +2382,7 @@ all_litellm_params = [
"litellm_session_id",
"use_litellm_proxy",
"prompt_label",
+ "shared_session",
] + list(StandardCallbackDynamicParams.__annotations__.keys()) + list(CustomPricingLiteLLMParams.model_fields.keys())
diff --git a/tests/test_litellm/test_aembedding_session_reuse_e2e.py b/tests/test_litellm/test_aembedding_session_reuse_e2e.py
new file mode 100644
index 00000000000..05cfb13bbf2
--- /dev/null
+++ b/tests/test_litellm/test_aembedding_session_reuse_e2e.py
@@ -0,0 +1,61 @@
+"""
+Regression test for commit 819a6b5f18
+
+Ensures shared_session is in all_litellm_params to prevent
+"Object of type ClientSession is not JSON serializable" errors.
+"""
+import os
+import sys
+import inspect
+
+sys.path.insert(0, os.path.abspath("../../.."))
+
+from litellm.types.utils import all_litellm_params
+
+
+def test_shared_session_in_all_litellm_params():
+ """
+ CRITICAL: shared_session must be in all_litellm_params.
+
+ If missing, it gets passed to provider APIs causing JSON serialization errors.
+ Regression test for commit 819a6b5f18.
+ """
+ assert "shared_session" in all_litellm_params
+
+
+def test_openai_embedding_passes_shared_session():
+ """
+ Verify shared_session flows through the complete call chain.
+
+ Full chain: litellm.embedding() -> OpenAI.embedding() -> _get_openai_client()
+ -> AsyncHTTPHandler -> _create_async_transport() -> _create_aiohttp_transport()
+ """
+ import litellm
+ from litellm.llms.openai.openai import OpenAIChatCompletion
+ from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
+
+ # Step 1: litellm.embedding() extracts and passes shared_session
+ main_source = inspect.getsource(litellm.embedding)
+ assert 'shared_session' in main_source
+
+ # Step 2: OpenAI handlers pass it forward
+ aembedding_source = inspect.getsource(OpenAIChatCompletion.aembedding)
+ embedding_source = inspect.getsource(OpenAIChatCompletion.embedding)
+ assert 'shared_session=shared_session' in aembedding_source
+ assert 'shared_session=shared_session' in embedding_source
+
+ # Step 3: _get_openai_client passes it to AsyncHTTPHandler
+ client_source = inspect.getsource(OpenAIChatCompletion._get_openai_client)
+ assert 'shared_session' in client_source
+
+ # Step 4: AsyncHTTPHandler.create_client passes it to _create_async_transport
+ create_client_source = inspect.getsource(AsyncHTTPHandler.create_client)
+ assert 'shared_session=shared_session' in create_client_source
+
+ # Step 5: _create_async_transport passes it to _create_aiohttp_transport
+ async_transport_source = inspect.getsource(AsyncHTTPHandler._create_async_transport)
+ assert 'shared_session=shared_session' in async_transport_source
+
+ # Step 6: _create_aiohttp_transport uses it
+ aiohttp_transport_source = inspect.getsource(AsyncHTTPHandler._create_aiohttp_transport)
+ assert 'shared_session' in aiohttp_transport_source