Files
langgraph/libs/sdk-py/tests/integration/test_assistants.py
T
Elior Nataf LackritzandGitHub ea5f9cc9fb chore: enforce PLC0415 in tests for the remaining packages (#8547)
Follow-up to #8540, which turned on `PLC0415` (import-outside-top-level)
for checkpoint-postgres and checkpoint-sqlite. This does the remaining
six packages: checkpoint, checkpoint-conformance, langgraph, prebuilt,
cli, sdk-py.

Scoped to tests, per @sydney-runkle's call on #8540: library code is
exempted with `per-file-ignores`, since it still has deferred imports
nobody has reviewed and mixing that in would make this hard to read.

## What changed

Function-level imports across 56 test files moved to module level. Nine
could not move and carry an explicit `# noqa: PLC0415` with a reason:

| File | Why it stays local |
|---|---|
| `libs/langgraph/tests/test_deprecation.py` (4) | the import has to run
inside `pytest.warns` for the warning to be observed |
| `libs/langgraph/tests/test_serde_allowlist.py` | try/except guard,
skips when langchain_core is absent |
| `libs/langgraph/tests/test_delta_channel_benchmark.py` | optional
psycopg probe |
| `libs/checkpoint/tests/test_conformance_delta.py` (3) | protected by a
module-level `pytest.importorskip`; hoisting past the guard turns a skip
into a collection error |

That last one is the trap: an import moved above `pytest.importorskip`
silently defeats the guard. I hit it locally and it turned the skip into
a `ModuleNotFoundError` at collection. Every file with an `importorskip`
or `except ImportError` was checked by hand for this.

## Verification

`make lint` and `make test` in each of the six:

| Package | Tests |
|---|---|
| checkpoint | 156 passed, 17 skipped |
| checkpoint-conformance | 1 passed |
| langgraph | 1968 passed, 4 skipped |
| prebuilt | 284 passed |
| cli | 336 passed |
| sdk-py | 493 passed |

Also confirmed the rule actually fires: a throwaway test file with a
function-level import is flagged in all six packages, and the source
exemption holds.
2026-08-07 09:40:18 -04:00

122 lines
3.9 KiB
Python

"""`AssistantsClient` against the integration API.
Covers the CRUD round-trip (create / get / update / delete), search by
metadata, and the graph introspection helpers (`get_graph`,
`get_schemas`). Both async and sync.
"""
from __future__ import annotations
import pytest
from langgraph_sdk._async.assistants import AssistantsClient
from langgraph_sdk._async.http import HttpClient
from langgraph_sdk._sync.assistants import SyncAssistantsClient
from langgraph_sdk._sync.http import SyncHttpClient
from .conftest import ASSISTANT_ID
pytestmark = pytest.mark.integration
def _async_assistants(raw):
return AssistantsClient(HttpClient(raw))
def _sync_assistants(raw):
return SyncAssistantsClient(SyncHttpClient(raw))
async def test_assistants_crud_async(async_threads) -> None:
_, raw = async_threads
client = _async_assistants(raw)
created = await client.create(
graph_id=ASSISTANT_ID,
metadata={"suite": "integration", "label": "crud-async"},
name="crud-async",
)
aid = created["assistant_id"]
try:
fetched = await client.get(aid)
assert fetched["assistant_id"] == aid
assert fetched["graph_id"] == ASSISTANT_ID
updated = await client.update(
aid, metadata={"suite": "integration", "label": "crud-async-updated"}
)
assert updated["metadata"]["label"] == "crud-async-updated"
results = await client.search(metadata={"label": "crud-async-updated"})
assert any(a["assistant_id"] == aid for a in results)
finally:
await client.delete(aid)
def test_assistants_crud_sync(sync_threads) -> None:
_, raw = sync_threads
client = _sync_assistants(raw)
created = client.create(
graph_id=ASSISTANT_ID,
metadata={"suite": "integration", "label": "crud-sync"},
name="crud-sync",
)
aid = created["assistant_id"]
try:
fetched = client.get(aid)
assert fetched["assistant_id"] == aid
assert fetched["graph_id"] == ASSISTANT_ID
updated = client.update(
aid, metadata={"suite": "integration", "label": "crud-sync-updated"}
)
assert updated["metadata"]["label"] == "crud-sync-updated"
results = client.search(metadata={"label": "crud-sync-updated"})
assert any(a["assistant_id"] == aid for a in results)
finally:
client.delete(aid)
async def test_assistants_graph_introspection_async(async_threads) -> None:
_, raw = async_threads
client = _async_assistants(raw)
# Introspection endpoints require a UUID. langgraph-api auto-creates
# one assistant per registered graph on startup; look it up by graph_id.
matches = await client.search(graph_id=ASSISTANT_ID, limit=1)
assert matches, f"no auto-created assistant for graph_id={ASSISTANT_ID!r}"
aid = matches[0]["assistant_id"]
graph = await client.get_graph(aid)
node_ids = [n["id"] for n in graph.get("nodes", [])]
assert "stream_message" in node_ids
assert "ask_human" in node_ids
graph_xray = await client.get_graph(aid, xray=True)
assert "nodes" in graph_xray and "edges" in graph_xray
schemas = await client.get_schemas(aid)
# Just verify the shape rather than exact field names (server-side
# schema generation may evolve).
assert "state_schema" in schemas
def test_assistants_graph_introspection_sync(sync_threads) -> None:
_, raw = sync_threads
client = _sync_assistants(raw)
matches = client.search(graph_id=ASSISTANT_ID, limit=1)
assert matches, f"no auto-created assistant for graph_id={ASSISTANT_ID!r}"
aid = matches[0]["assistant_id"]
graph = client.get_graph(aid)
node_ids = [n["id"] for n in graph.get("nodes", [])]
assert "stream_message" in node_ids
assert "ask_human" in node_ids
graph_xray = client.get_graph(aid, xray=True)
assert "nodes" in graph_xray and "edges" in graph_xray
schemas = client.get_schemas(aid)
assert "state_schema" in schemas