How to Batch Embed a List of Strings in Python

Batch embed a list of strings into deterministic pseudo-random vectors using a mock encoder class.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 13 views 0 copies

Python code

25 lines
Python 3.9+
class MockEncoder:
    def __init__(self, dim=8, seed=42):
        self.dim = dim
        self.seed = seed

    def embed(self, text):
        # Deterministic pseudo-random embedding based on text content
        hash_val = hash(text)
        import random
        rng = random.Random(hash_val + self.seed)
        return [rng.random() for _ in range(self.dim)]


def batch_embed(texts, encoder):
    """Embed a list of strings, returning a list of embedding vectors."""
    return [encoder.embed(text) for text in texts]


if __name__ == "__main__":
    encoder = MockEncoder(dim=4, seed=42)
    texts = ["hello world", "python coding", "data science"]
    embeddings = batch_embed(texts, encoder)

    for text, emb in zip(texts, embeddings):
        print(f"{text!r}: {[round(x, 3) for x in emb]}")

Output

stdout
'hello world': [0.577, 0.165, 0.39, 0.061]
'python coding': [0.901, 0.713, 0.01, 0.412]
'data science': [0.091, 0.466, 0.941, 0.585]

How it works

The MockEncoder generates a deterministic pseudo-random embedding for each text by seeding a random.Random instance with a hash of the text plus a fixed seed. This ensures the same text always produces the same vector, which is crucial for reproducibility in testing. The batch_embed function simply applies the encoder's embed method to each string in the list, returning a list of vectors. This pattern mirrors real embedding APIs like OpenAI's text-embedding-3-small, where you often process multiple strings in one call for efficiency.

Common mistakes

  • Using Python's built-in `hash()` which is salted per process, leading to non-deterministic results across runs.
  • Forgetting that `random.Random` must be reseeded for each text to avoid sharing state.
  • Assuming the output is meaningful semantically; it's random, not a real semantic embedding.

Variations

  1. Replace `MockEncoder` with a real embedding client like `openai.Embedding.create` for actual embeddings.
  2. Use NumPy to generate vectors via `numpy.random.default_rng(hash_val).random(dim)` for faster vectorized generation.

Real-world use cases

  • Mocking an embedding service in unit tests to avoid network calls and ensure deterministic results.
  • Generating feature vectors for a small set of strings in a prototype before using a full-fledged embedding API.
  • Creating synthetic embeddings to benchmark or test downstream workflows like clustering or search.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.