-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Add optional idempotency support to batches API #3171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattf
wants to merge
2
commits into
llamastack:main
Choose a base branch
from
mattf:add-idempotency-to-reference-batches-impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+351
−64
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
|
||
""" | ||
Integration tests for batch idempotency functionality using the OpenAI client library. | ||
|
||
This module tests the idempotency feature in the batches API using the OpenAI-compatible | ||
client interface. These tests verify that the idempotency key (idempotency_key) works correctly | ||
in a real client-server environment. | ||
|
||
Test Categories: | ||
1. Successful Idempotency: Same key returns same batch with identical parameters | ||
- test_idempotent_batch_creation_successful: Verifies that requests with the same | ||
idempotency key return identical batches, even with different metadata order | ||
|
||
2. Conflict Detection: Same key with conflicting parameters raises HTTP 409 errors | ||
- test_idempotency_conflict_with_different_params: Verifies that reusing an idempotency key | ||
with truly conflicting parameters (both file ID and metadata values) raises ConflictError | ||
""" | ||
|
||
import time | ||
|
||
import pytest | ||
from openai import ConflictError | ||
|
||
|
||
class TestBatchesIdempotencyIntegration: | ||
"""Integration tests for batch idempotency using OpenAI client.""" | ||
|
||
def test_idempotent_batch_creation_successful(self, openai_client): | ||
"""Test that identical requests with same idempotency key return the same batch.""" | ||
batch1 = openai_client.batches.create( | ||
input_file_id="bogus-id", | ||
endpoint="/v1/chat/completions", | ||
completion_window="24h", | ||
metadata={ | ||
"test_type": "idempotency_success", | ||
"purpose": "integration_test", | ||
}, | ||
extra_body={"idempotency_key": "test-idempotency-token-1"}, | ||
) | ||
|
||
# sleep to ensure different timestamps | ||
time.sleep(1) | ||
|
||
batch2 = openai_client.batches.create( | ||
input_file_id="bogus-id", | ||
endpoint="/v1/chat/completions", | ||
completion_window="24h", | ||
metadata={ | ||
"purpose": "integration_test", | ||
"test_type": "idempotency_success", | ||
}, # Different order | ||
extra_body={"idempotency_key": "test-idempotency-token-1"}, | ||
) | ||
|
||
assert batch1.id == batch2.id | ||
assert batch1.input_file_id == batch2.input_file_id | ||
assert batch1.endpoint == batch2.endpoint | ||
assert batch1.completion_window == batch2.completion_window | ||
assert batch1.metadata == batch2.metadata | ||
assert batch1.created_at == batch2.created_at | ||
|
||
def test_idempotency_conflict_with_different_params(self, openai_client): | ||
"""Test that using same idempotency key with different params raises conflict error.""" | ||
batch1 = openai_client.batches.create( | ||
input_file_id="bogus-id-1", | ||
endpoint="/v1/chat/completions", | ||
completion_window="24h", | ||
metadata={"test_type": "conflict_test_1"}, | ||
extra_body={"idempotency_key": "conflict-token"}, | ||
) | ||
|
||
with pytest.raises(ConflictError) as exc_info: | ||
openai_client.batches.create( | ||
input_file_id="bogus-id-2", # Different file ID | ||
endpoint="/v1/chat/completions", | ||
completion_window="24h", | ||
metadata={"test_type": "conflict_test_2"}, # Different metadata | ||
extra_body={"idempotency_key": "conflict-token"}, # Same token | ||
) | ||
|
||
assert exc_info.value.status_code == 409 | ||
assert "conflict" in str(exc_info.value).lower() | ||
|
||
retrieved_batch = openai_client.batches.retrieve(batch1.id) | ||
assert retrieved_batch.id == batch1.id | ||
assert retrieved_batch.input_file_id == "bogus-id-1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
|
||
"""Shared fixtures for batches provider unit tests.""" | ||
|
||
import tempfile | ||
from pathlib import Path | ||
from unittest.mock import AsyncMock | ||
|
||
import pytest | ||
|
||
from llama_stack.providers.inline.batches.reference.batches import ReferenceBatchesImpl | ||
from llama_stack.providers.inline.batches.reference.config import ReferenceBatchesImplConfig | ||
from llama_stack.providers.utils.kvstore import kvstore_impl | ||
from llama_stack.providers.utils.kvstore.config import SqliteKVStoreConfig | ||
|
||
|
||
@pytest.fixture | ||
async def provider(): | ||
"""Create a test provider instance with temporary database.""" | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
db_path = Path(tmpdir) / "test_batches.db" | ||
kvstore_config = SqliteKVStoreConfig(db_path=str(db_path)) | ||
config = ReferenceBatchesImplConfig(kvstore=kvstore_config) | ||
|
||
# Create kvstore and mock APIs | ||
kvstore = await kvstore_impl(config.kvstore) | ||
mock_inference = AsyncMock() | ||
mock_files = AsyncMock() | ||
mock_models = AsyncMock() | ||
|
||
provider = ReferenceBatchesImpl(config, mock_inference, mock_files, mock_models, kvstore) | ||
await provider.initialize() | ||
|
||
# unit tests should not require background processing | ||
provider.process_batches = False | ||
|
||
yield provider | ||
|
||
await provider.shutdown() | ||
|
||
|
||
@pytest.fixture | ||
def sample_batch_data(): | ||
"""Sample batch data for testing.""" | ||
return { | ||
"input_file_id": "file_abc123", | ||
"endpoint": "/v1/chat/completions", | ||
"completion_window": "24h", | ||
"metadata": {"test": "true", "priority": "high"}, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default batch id use's a 16 char hex section, is there a reason to use a different length here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secret way to tell the difference. happy to align them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh that's fine then. i personally like to add a prefix for those reasons (OpenAI follows this but they don't expose an idempotency key) but different size is okay.