test_embeddings_respects_token_limit() — langchain Function Reference
Architecture documentation for the test_embeddings_respects_token_limit() function in test_base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 3c725a77_ea3a_1ada_67a5_b6d4d410e1ac["test_embeddings_respects_token_limit()"] 1b2604cc_dec7_a9eb_ce6b_1cbab7a6dc2e["test_base.py"] 3c725a77_ea3a_1ada_67a5_b6d4d410e1ac -->|defined in| 1b2604cc_dec7_a9eb_ce6b_1cbab7a6dc2e style 3c725a77_ea3a_1ada_67a5_b6d4d410e1ac fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/openai/tests/unit_tests/embeddings/test_base.py lines 103–150
def test_embeddings_respects_token_limit() -> None:
"""Test that embeddings respect the 300k token per request limit."""
# Create embeddings instance
embeddings = OpenAIEmbeddings(
model="text-embedding-ada-002", api_key=SecretStr("test-key")
)
call_counts = []
def mock_create(**kwargs: Any) -> Mock:
input_ = kwargs["input"]
# Track how many tokens in this call
if isinstance(input_, list):
total_tokens = sum(
len(t) if isinstance(t, list) else len(t.split()) for t in input_
)
call_counts.append(total_tokens)
# Verify this call doesn't exceed limit
assert total_tokens <= 300000, (
f"Batch exceeded token limit: {total_tokens} tokens"
)
# Return mock response
mock_response = Mock()
mock_response.model_dump.return_value = {
"data": [
{"embedding": [0.1] * 1536}
for _ in range(len(input_) if isinstance(input_, list) else 1)
]
}
return mock_response
embeddings.client.create = mock_create
# Create a scenario that would exceed 300k tokens in a single batch
# with default chunk_size=1000
# Simulate 500 texts with ~1000 tokens each = 500k tokens total
large_texts = ["word " * 1000 for _ in range(500)]
# This should not raise an error anymore
embeddings.embed_documents(large_texts)
# Verify we made multiple API calls to respect the limit
assert len(call_counts) > 1, "Should have split into multiple batches"
# Verify each call respected the limit
for count in call_counts:
assert count <= 300000, f"Batch exceeded limit: {count}"
Domain
Subdomains
Source
Frequently Asked Questions
What does test_embeddings_respects_token_limit() do?
test_embeddings_respects_token_limit() is a function in the langchain codebase, defined in libs/partners/openai/tests/unit_tests/embeddings/test_base.py.
Where is test_embeddings_respects_token_limit() defined?
test_embeddings_respects_token_limit() is defined in libs/partners/openai/tests/unit_tests/embeddings/test_base.py at line 103.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free