@embedding_span¶
Wraps an embedding function with an OpenInference EMBEDDING span. Captures the input text, model name, and resulting embedding vector as span attributes.
Decorator / Wrapper¶
Signature¶
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str \| None |
None |
Span name. Defaults to the decorated function's name. |
model_name |
str \| None |
None |
Embedding model name to record as a span attribute (e.g., "text-embedding-3-small"). |
query_arg |
str |
"text" |
Name of the keyword argument containing the text to embed. Falls back to the first positional str argument. |
Span Attributes¶
| Attribute | Value |
|---|---|
openinference.span.kind |
"EMBEDDING" |
embedding.model_name |
Model name (set when model_name parameter is provided) |
input.value |
The text being embedded (from query_arg kwarg or first str positional argument) |
embedding.embeddings |
JSON-encoded embedding vector (set when the function returns list[float]) |
Embedding vector format
The embedding.embeddings attribute is a JSON string with the format:
Example¶
from coalex.ext.embedding import embedding_span
from openai import OpenAI
client = OpenAI()
@embedding_span(name="embed_query", model_name="text-embedding-3-small")
def embed(text: str) -> list[float]:
response = client.embeddings.create(
model="text-embedding-3-small",
input=text,
)
return response.data[0].embedding
# Usage
vector = embed(text="What is the refund policy?")
# Span "embed_query" is emitted with:
# openinference.span.kind = "EMBEDDING"
# embedding.model_name = "text-embedding-3-small"
# input.value = "What is the refund policy?"
# embedding.embeddings = '[{"embedding.vector": [0.1, ...], "embedding.text": "What is the refund policy?"}]'
import { embeddingSpan } from "@coalex-ai/sdk/ext";
import OpenAI from "openai";
const client = new OpenAI();
const embed = embeddingSpan(
{ name: "embed_query", modelName: "text-embedding-3-small" },
async (text: string): Promise<number[]> => {
const response = await client.embeddings.create({
model: "text-embedding-3-small",
input: text,
});
return response.data[0].embedding;
},
);
// Usage
const vector = await embed("What is the refund policy?");
// Span "embed_query" is emitted with:
// openinference.span.kind = "EMBEDDING"
// embedding.model_name = "text-embedding-3-small"
// input.value = "What is the refund policy?"
// embedding.embeddings = '[{"embedding.vector": [0.1, ...], "embedding.text": "What is the refund policy?"}]'
Async Support¶
EmbeddingSpan Context Manager / Class¶
For fine-grained control over when attributes are set on the span, use the EmbeddingSpan context manager (Python) or class (TypeScript).
Signature¶
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str / string |
"embedding" |
Span name. |
model_name / modelName |
str \| None / string? |
None / undefined |
Embedding model name to record. |
Methods¶
| Python | TypeScript | Description |
|---|---|---|
set_text(text) |
setText(text) |
Set the input.value attribute on the span. |
set_vector(vector, text="") |
setVector(vector, text?) |
Set the embedding.embeddings attribute with the vector and associated text. |
| -- | start() |
Start the span (TypeScript only; Python uses with statement). |
| -- | end() |
End the span (TypeScript only; Python uses with statement). |
Example¶
import { EmbeddingSpan } from "@coalex-ai/sdk/ext";
const e = new EmbeddingSpan("embed_document", "text-embedding-3-small");
e.start();
try {
const text = "Metformin is used for type 2 diabetes management.";
e.setText(text);
// Call your embedding API
const vector = await embeddingApi.embed(text);
e.setVector(vector, text);
} finally {
e.end();
}
Batch Embedding Example¶
from coalex.ext.embedding import embedding_span, EmbeddingSpan
@embedding_span(name="embed_single", model_name="text-embedding-3-small")
def embed_text(text: str) -> list[float]:
response = client.embeddings.create(model="text-embedding-3-small", input=text)
return response.data[0].embedding
# For batch embedding, use the context manager for more control
def embed_batch(texts: list[str]) -> list[list[float]]:
vectors = []
for text in texts:
with EmbeddingSpan("embed_batch_item", model_name="text-embedding-3-small") as e:
e.set_text(text)
response = client.embeddings.create(model="text-embedding-3-small", input=text)
vector = response.data[0].embedding
e.set_vector(vector, text)
vectors.append(vector)
return vectors
With Retrieval Pipeline¶
A common pattern is to embed queries before searching a vector store:
import coalex
from coalex.ext.embedding import embedding_span
from coalex.ext.retrieval import retrieval_span, Document
@embedding_span(name="embed_query", model_name="text-embedding-3-small")
def embed_query(text: str) -> list[float]:
response = client.embeddings.create(model="text-embedding-3-small", input=text)
return response.data[0].embedding
@retrieval_span(name="vector_search", query_arg="query")
def search(query: str) -> list[Document]:
vector = embed_query(text=query)
results = vector_store.search(vector, top_k=10)
return [Document(content=r.text, id=r.id, score=r.score) for r in results]
with coalex.coalex_context(agent_id="search-agent"):
docs = search(query="diabetes treatment guidelines")
# Trace contains:
# coalex.invocation
# └── vector_search (RETRIEVER)
# └── embed_query (EMBEDDING)
API Reference¶
coalex.ext.embedding ¶
Embedding span decorator and context manager.
Classes¶
EmbeddingSpan ¶
Context manager for instrumenting an embedding call as an EMBEDDING span.
Example::
with EmbeddingSpan("embed", model_name="text-embedding-3-small") as e:
e.set_text(text)
vector = embed(text)
e.set_vector(vector, text)
Source code in coalex/ext/embedding.py
Functions¶
embedding_span ¶
embedding_span(
name: str | None = None,
model_name: str | None = None,
query_arg: str = "text",
) -> Callable[[F], F]
Decorator that wraps an embedding function with an OpenInference EMBEDDING span.
Supports both sync and async functions. The span captures:
- openinference.span.kind = EMBEDDING
- embedding.model_name — if provided
- input.value — text being embedded
- embedding.embeddings — JSON-encoded vector if function returns list[float]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Span name. Defaults to the function name. |
None
|
model_name
|
str | None
|
Embedding model name to record. |
None
|
query_arg
|
str
|
Name of the kwarg containing the text to embed. |
'text'
|