{"record":{"id":"14aa44235c2a4028","repo":"microsoft/semantic-kernel","slug":"limit-must-be-less-than-or-equal-to-max-query-wit","errorCode":null,"errorMessage":"Limit must be less than or equal to {MAX_QUERY_WITHOUT_METADATA_BATCH_SIZE}","messagePattern":"Limit must be less than or equal to (.+?)","errorType":"exception","errorClass":"ServiceInvalidRequestError","httpStatus":400,"severity":"error","filePath":"python/semantic_kernel/connectors/memory_stores/pinecone/pinecone_memory_store.py","lineNumber":354,"sourceCode":"        \"\"\"Gets the nearest matches to an embedding using cosine similarity.\n\n        Args:\n            collection_name (str): The name of the collection to get the nearest matches from.\n            embedding (ndarray): The embedding to find the nearest matches to.\n            limit (int): The maximum number of matches to return.\n            min_relevance_score (float): The minimum relevance score of the matches. (default: {0.0})\n            with_embeddings (bool): Whether to include the embeddings in the results. (default: {False})\n\n        Returns:\n            List[Tuple[MemoryRecord, float]]: The records and their relevance scores.\n        \"\"\"\n        if not await self.does_collection_exist(collection_name):\n            raise ServiceResourceNotFoundError(f\"Collection '{collection_name}' does not exist\")\n\n        collection = self.pinecone.Index(collection_name)\n\n        if limit > MAX_QUERY_WITHOUT_METADATA_BATCH_SIZE:\n            raise ServiceInvalidRequestError(\n                \"Limit must be less than or equal to \" + f\"{MAX_QUERY_WITHOUT_METADATA_BATCH_SIZE}\"\n            )\n        if limit > MAX_QUERY_WITH_METADATA_BATCH_SIZE:\n            query_response = collection.query(\n                vector=embedding.tolist(),\n                top_k=limit,\n                include_values=False,\n                include_metadata=False,\n            )\n            keys = [match.id for match in query_response.matches]\n            fetch_response = await self.__get_batch(collection_name, keys, with_embeddings)\n            vectors = fetch_response.vectors\n            for match in query_response.matches:\n                vectors[match.id].update(match)\n            matches = [vectors[key] for key in vectors]\n        else:\n            query_response = collection.query(\n                vector=embedding.tolist(),","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/pinecone/pinecone_memory_store.py#L336-L372","documentation":"Raised in PineconeMemoryStore.get_nearest_matches() when limit exceeds MAX_QUERY_WITHOUT_METADATA_BATCH_SIZE (10000). ServiceInvalidRequestError (a subclass of ServiceResponseException) fires after the collection-existence check but before the query. This enforces Pinecone's documented top_k ceiling for queries without metadata.","triggerScenarios":"Calling get_nearest_matches(..., limit=N) with N > 10000. Note the code checks the no-metadata ceiling first even when with_embeddings/with_metadata is requested; a higher ceiling (MAX_QUERY_WITH_METADATA_BATCH_SIZE=1000) is checked next.","commonSituations":"Caller passes an unbounded 'top N' from user input; pagination limit copied from a different backend; desire to retrieve the whole index in one call.","solutions":["Reduce limit to <= 10000 (and <= 1000 when metadata is requested).","Cap user-supplied limit at the boundary before calling.","Paginate via multiple smaller queries if more results are needed.","Migrate to PineconeStore + Collection."],"exampleFix":"// before\nmatches = await store.get_nearest_matches(\"my_col\", emb, limit=50000, with_embeddings=False)\n\n// after\nlimit = min(user_limit, 10000)\nmatches = await store.get_nearest_matches(\"my_col\", emb, limit=limit, with_embeddings=False)","handlingStrategy":"validation","validationCode":"from semantic_kernel.connectors.memory_stores.pinecone.pinecone_memory_store import MAX_QUERY_WITHOUT_METADATA_BATCH_SIZE\nlimit = min(limit, MAX_QUERY_WITHOUT_METADATA_BATCH_SIZE)\nreturn await store.get_nearest_matches(collection_name, embedding, limit, with_embeddings)","typeGuard":"def is_valid_query_limit(limit: int, with_metadata: bool) -> bool:\n    return limit <= (1000 if with_metadata else 10000)","tryCatchPattern":"from semantic_kernel.exceptions import ServiceInvalidRequestError\ntry:\n    return await store.get_nearest_matches(collection_name, embedding, limit, with_embeddings)\nexcept ServiceInvalidRequestError as e:\n    raise ValueError(f\"limit too large for Pinecone: {e}\") from e","preventionTips":["Cap user-supplied limit at the Pinecone ceiling before calling.","Paginate instead of requesting huge top_k in one call.","Remember the metadata path has a lower ceiling (1000)."],"tags":["pinecone","memory-store","deprecated","limit-exceeded","search","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}