Files
litellm/tests/test_litellm/test_aembedding_session_reuse_e2e.py
T
Alexsander Hamir 2c9356c437 [Fix] - shared session parsing and usage issue (#15440)
* Fix: Add shared_session to all_litellm_params to prevent JSON serialization error

The shared_session parameter (aiohttp.ClientSession) was being passed through
to provider API calls, causing "Object of type ClientSession is not JSON
serializable" errors during embedding requests.

Added shared_session to the all_litellm_params list so it's properly filtered
out as a LiteLLM-internal parameter and not passed to the provider's API.

* Fix: Add shared_session support for embedding calls with connection pooling

The shared_session parameter was not being properly handled in embedding calls,
causing it to be passed through to provider API requests where it's not needed.

Changes:
- Added shared_session to all_litellm_params to filter it from provider API request body
- Extract shared_session in main embedding() function and pass it explicitly
- Updated OpenAI embedding handlers (embedding() and aembedding()) to accept shared_session
- Pass shared_session to _get_openai_client for HTTP client creation

This enables proper connection pooling for embedding requests when shared_session
is provided, improving performance for high-throughput scenarios.

* test: add regression test for shared_session in embedding calls

Add comprehensive test to prevent JSON serialization errors when using
shared_session.

The test verifies two critical aspects:
1. shared_session is in all_litellm_params to prevent "Object of type
   ClientSession is not JSON serializable" errors
2. shared_session flows through the complete call chain across 6 layers:
   - litellm.embedding()
   - OpenAI.embedding/aembedding()
   - _get_openai_client()
   - AsyncHTTPHandler.create_client()
   - _create_async_transport()
   - _create_aiohttp_transport()

Similar to test_acompletion_session_reuse_e2e.py but focused on
embedding endpoints. Uses inspect.getsource() to verify the parameter
is not only accepted but actually passed through each layer.
2025-10-10 19:26:53 -07:00

62 lines
2.4 KiB
Python

"""
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