You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the concrete implementation of EmbeddingGenerator / AsyncEmbeddingGenerator that lives inside the azure-cosmos-embedding package. It wraps the openai.AzureOpenAI (sync) and openai.AsyncAzureOpenAI (async) clients to generate text embeddings, and can be constructed from a VectorEmbeddingPolicyEmbeddingSource or from explicit parameters.
Reference: the .NET sibling is AzureOpenAIEmbeddingGenerator in Microsoft.Azure.Cosmos.Embedding (see Azure/azure-cosmos-dotnet-v3#5842).
Public API
# azure/cosmos/embedding/_azure_openai_embedding_generator.pyfromazure.cosmosimportEmbeddingGenerator, AsyncEmbeddingGeneratorfromazure.cosmos._modelsimportEmbeddingSourceclassAzureOpenAIEmbeddingGenerator:
""" Implements both EmbeddingGenerator (sync) and AsyncEmbeddingGenerator (async). Can be constructed directly or from a container's VectorEmbeddingPolicy EmbeddingSource. """@classmethoddeffrom_embedding_source(
cls,
source: EmbeddingSource,
api_key: str|None=None,
credential=None, # azure.core.credentials.TokenCredential
) ->"AzureOpenAIEmbeddingGenerator": ...
def__init__(
self,
endpoint: str,
deployment_name: str,
dimensions: int,
api_key: str|None=None,
credential=None, # azure.core.credentials.TokenCredential for Entra authmax_batch_size: int=100,
) ->None: ...
defgenerate_embeddings(
self, texts: Sequence[str]
) ->Sequence[Sequence[float]]: ... # sync — EmbeddingGenerator Protocolasyncdefgenerate_embeddings_async(
self, texts: Sequence[str]
) ->Sequence[Sequence[float]]: ... # async — AsyncEmbeddingGenerator Protocol
Behavioral requirements
Auth: ApiKey via openai.AzureOpenAI(api_key=api_key) or Entra via azure.identityget_token injected as AzureOpenAI(azure_ad_token_provider=...).
Batching: if len(texts) > max_batch_size, split into chunks and concatenate results in order.
Dimensions: pass dimensions=self.dimensions to the embeddings API call so output size matches the container policy.
Return type: list[list[float]] — each inner list is one embedding vector.
Error handling: surface openai.APIError / openai.APIConnectionError as azure.cosmos.exceptions.CosmosHttpResponseError (or a new CosmosEmbeddingError) with the original exception as __cause__.
from_embedding_source: reads endpoint, deploymentName, dimensions, and authType from the EmbeddingSource TypedDict; api_key or credential must be supplied by the caller (not stored in the policy for security reasons).
Thread safety: AzureOpenAI client is thread-safe; concurrent sync calls are supported.
Event loop: async variant uses AsyncAzureOpenAI; do not mix sync client into an async context.
Acceptance criteria
Unit tests (in package's tests/ folder) cover:
Happy path sync: single batch, returns correct shape list[list[float]].
Happy path async: same.
Multi-batch: input > max_batch_size, two API calls made, results concatenated in order.
APIError → wrapped as CosmosHttpResponseError (or CosmosEmbeddingError).
from_embedding_source constructs correctly from a typed EmbeddingSource dict.
AzureOpenAIEmbeddingGenerator is exported from azure.cosmos.embedding.
[Cosmos] [Embedding V0]
AzureOpenAIEmbeddingGenerator— provider implementationParent: #46729
Background
This is the concrete implementation of
EmbeddingGenerator/AsyncEmbeddingGeneratorthat lives inside theazure-cosmos-embeddingpackage. It wraps theopenai.AzureOpenAI(sync) andopenai.AsyncAzureOpenAI(async) clients to generate text embeddings, and can be constructed from aVectorEmbeddingPolicyEmbeddingSourceor from explicit parameters.Reference: the .NET sibling is
AzureOpenAIEmbeddingGeneratorinMicrosoft.Azure.Cosmos.Embedding(see Azure/azure-cosmos-dotnet-v3#5842).Public API
Behavioral requirements
openai.AzureOpenAI(api_key=api_key)or Entra viaazure.identityget_tokeninjected asAzureOpenAI(azure_ad_token_provider=...).len(texts) > max_batch_size, split into chunks and concatenate results in order.dimensions=self.dimensionsto the embeddings API call so output size matches the container policy.list[list[float]]— each inner list is one embedding vector.openai.APIError/openai.APIConnectionErrorasazure.cosmos.exceptions.CosmosHttpResponseError(or a newCosmosEmbeddingError) with the original exception as__cause__.from_embedding_source: readsendpoint,deploymentName,dimensions, andauthTypefrom theEmbeddingSourceTypedDict;api_keyorcredentialmust be supplied by the caller (not stored in the policy for security reasons).AzureOpenAIclient is thread-safe; concurrent sync calls are supported.AsyncAzureOpenAI; do not mix sync client into an async context.Acceptance criteria
tests/folder) cover:list[list[float]].max_batch_size, two API calls made, results concatenated in order.APIError→ wrapped asCosmosHttpResponseError(orCosmosEmbeddingError).from_embedding_sourceconstructs correctly from a typedEmbeddingSourcedict.AzureOpenAIEmbeddingGeneratoris exported fromazure.cosmos.embedding.isinstance(gen, EmbeddingGenerator)returnsTrue(Protocolruntime_checkable).embedding_generatortocontainer.query_itemsin an emulator test (covered by [Cosmos] [Embedding V0] Emulator tests + sample for embedding generation #46736).Files likely touched
sdk/cosmos/azure-cosmos-embedding/azure/cosmos/embedding/_azure_openai_embedding_generator.py(new)sdk/cosmos/azure-cosmos-embedding/azure/cosmos/embedding/__init__.pysdk/cosmos/azure-cosmos-embedding/tests/test_azure_openai_embedding_generator.py(new)Dependencies
EmbeddingSourceTypedDict (#NEW-A — VectorEmbeddingPolicy issue)EmbeddingGenerator/AsyncEmbeddingGeneratorprotocols ([Cosmos] [Embedding V0] Public surface: EmbeddingGenerator protocols + embedding_generator on query_items #46730)