{"record":{"id":"0ad1da0d640b4471","repo":"agentscope-ai/agentscope","slug":"embedding-model-returned-len-response-embeddings","errorCode":null,"errorMessage":"Embedding model returned {len(response.embeddings)} vectors for {len(chunks)} chunks.","messagePattern":"Embedding model returned (.+?) vectors for (.+?) chunks\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/agentscope/rag/_knowledge.py","lineNumber":341,"sourceCode":"        document_id = document_id or _generate_id()\n\n        await self.ensure_collection()\n\n        # Precedence: metadata_filter wins (security boundary), then\n        # chunk metadata, then document_metadata.  See docstring.\n        for chunk in chunks:\n            chunk.metadata = {\n                **(document_metadata or {}),\n                **chunk.metadata,\n                **(self._metadata_filter or {}),\n            }\n\n        response = await self._embedding_model(\n            [chunk.content for chunk in chunks],\n        )\n\n        if len(response.embeddings) != len(chunks):\n            raise RuntimeError(\n                f\"Embedding model returned {len(response.embeddings)} \"\n                f\"vectors for {len(chunks)} chunks.\",\n            )\n\n        records = [\n            VectorRecord(\n                vector=vector,\n                document_id=document_id,\n                chunk=chunk,\n            )\n            for vector, chunk in zip(response.embeddings, chunks)\n        ]\n        await self._vector_store.insert(self._collection, records)\n        return document_id\n\n    async def delete_document(self, document_id: str) -> None:\n        \"\"\"Remove every record for one source document.\n","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/rag/_knowledge.py#L323-L359","documentation":"After embedding all chunk contents, the knowledge base verifies the embedding model returned exactly one vector per chunk. A count mismatch means the embedding provider returned fewer/more vectors (batching bug, provider inconsistency, or a custom embedding model with wrong return shape), so insertion aborts.","triggerScenarios":"Calling insert_document/build_index with a custom _embedding_model whose __call__ returns an EmbeddingResponse with truncated embeddings, or a provider that drops empty-text inputs from its response.","commonSituations":"Custom embedding wrappers that filter empty strings; provider batch-size limits silently truncating results; empty chunk content causing the provider to skip a vector; version changes in the embedding response format.","solutions":["If using a custom embedding model, ensure it returns exactly one embedding per input, preserving order and count (pad/handle empty inputs rather than dropping them)","Filter out empty/whitespace chunks before insert_document","Reduce the batch/chunk count to isolate which inputs get dropped; log len(request) vs len(response.embeddings) in your wrapper","Retry — some providers intermittently truncate; if persistent, switch embedding model"],"exampleFix":"# before (custom model drops empty inputs)\nasync def __call__(self, texts):\n    texts = [t for t in texts if t]  # count mismatch!\n    ...\n\n# after\nasync def __call__(self, texts):\n    texts = [t or ' ' for t in texts]  # keep 1:1 mapping\n    ...","handlingStrategy":"validation","validationCode":"chunks = [c for c in chunks if c.content and c.content.strip()]  # avoid empty inputs\nawait kb.insert_document(chunks)","typeGuard":null,"tryCatchPattern":"try:\n    await kb.insert_document(chunks)\nexcept RuntimeError as e:\n    if 'vectors for' not in str(e):\n        raise\n    # split into smaller batches and retry to isolate provider truncation","preventionTips":["Custom embedding models must return exactly one vector per input in order","Filter empty chunks before insertion","Log input/output counts in embedding wrappers"],"tags":["agentscope","rag","embedding","vector-count","custom-model"],"backgroundTag":"embedding-count-mismatch","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}