Skip to content

Commit 9b03a75

Browse files
jhamonclaude
andcommitted
docs: fix async pc.index() usage — it's a coroutine, must be awaited
AsyncPinecone.index() is a coroutine that resolves to AsyncIndex, but several examples treated it as a synchronous factory. Every existing async sample either assigned the coroutine to a local without awaiting it (then crashed on the first method call with "'coroutine' object has no attribute upsert/query"), or used `async with pc.index(...) as index:` directly on the coroutine (which is also wrong — async with needs the resolved AsyncIndex, not the coroutine). Same bug we hit in the harness's _build_client_async_sdk2 earlier today. Fixes: - README.md async quickstart: add `await` to `pc.index(host=...)` - sync-vs-async.md: rewrite the prose ("pc.index() is a synchronous factory method — it does not need to be awaited") which had it exactly backwards on the async client; switch every async example to the two-step pattern `index = await pc.index(...); async with index: ...` for readability Sphinx build verified clean after the change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a54d581 commit 9b03a75

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ from pinecone import AsyncPinecone
6767
async def main():
6868
async with AsyncPinecone(api_key="your-api-key") as pc:
6969
desc = await pc.indexes.describe("movie-recommendations")
70-
index = pc.index(host=desc.host)
70+
index = await pc.index(host=desc.host)
7171
async with index:
7272
results = await index.query(
7373
vector=[0.012, -0.087, 0.153], # 1536-dim vector

‎docs/guides/sync-vs-async.md‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,23 @@ from pinecone import AsyncPinecone
5353
async def main() -> None:
5454
async with AsyncPinecone() as pc:
5555
desc = await pc.indexes.describe("product-search")
56-
index = pc.index(host=desc.host)
56+
index = await pc.index(host=desc.host)
5757

5858
await index.upsert(vectors=[("product-42", [0.1, 0.2, ...])])
5959
results = await index.query(vector=[0.1, 0.2, ...], top_k=5)
6060

6161
asyncio.run(main())
6262
```
6363

64-
`pc.index()` is a synchronous factory method — it does not need to be awaited. It
65-
returns an `AsyncIndex` that you use with `await`. The `AsyncIndex` itself manages its
66-
own HTTP session; close it with `async with index:` or `await index.close()`.
64+
`pc.index()` on `AsyncPinecone` is itself a coroutine — `await` it to get back an
65+
`AsyncIndex`. The returned `AsyncIndex` manages its own HTTP session; close it
66+
with `async with index:` or `await index.close()`.
6767

6868
```python
6969
async with AsyncPinecone() as pc:
7070
# Preferred: pass the host directly to skip a describe call
71-
async with pc.index(host="product-search-abc123.svc.pinecone.io") as index:
71+
index = await pc.index(host="product-search-abc123.svc.pinecone.io")
72+
async with index:
7273
results = await index.query(vector=[0.1, 0.2, ...], top_k=5)
7374
```
7475

@@ -108,7 +109,8 @@ async def main() -> None:
108109
async with AsyncPinecone() as pc:
109110
# Resolve the host first, then create the index client
110111
desc = await pc.indexes.describe("movie-recommendations")
111-
async with pc.index(host=desc.host) as index:
112+
index = await pc.index(host=desc.host)
113+
async with index:
112114
await index.upsert(vectors=[
113115
("movie-42", [0.012, -0.087, 0.153, ...]),
114116
("movie-99", [0.045, 0.021, -0.064, ...]),
@@ -151,7 +153,8 @@ import asyncio
151153
from pinecone import AsyncPinecone
152154

153155
async def upsert_batch(pc: AsyncPinecone, batch: list[tuple[str, list[float]]]) -> None:
154-
async with pc.index(host="product-search-abc123.svc.pinecone.io") as index:
156+
index = await pc.index(host="product-search-abc123.svc.pinecone.io")
157+
async with index:
155158
await index.upsert(vectors=batch)
156159

157160
async def main() -> None:
@@ -191,7 +194,8 @@ each `AsyncIndex` separately:
191194

192195
```python
193196
async with AsyncPinecone() as pc:
194-
async with pc.index(host="...") as index:
197+
index = await pc.index(host="...")
198+
async with index:
195199
await index.query(...)
196200
# index is closed here; pc is closed at the outer block exit
197201
```

0 commit comments

Comments
 (0)