langchain-ai / langchain-ai/langgraph
TypeError collecting serde allowlist for collections.abc generic containers
- Dominant language
- Python
- Stars
- 41.8k
- Forks
- 7.1k
- Avg merge
- 23h 7m
- Merged PRs (30d)
- 30
Description
### Checked other resources
- [x] This is a bug, not a usage question.
- [x] I used GitHub search to find a similar issue and did not find one for this serde allowlist path.
- [x] I included a self-contained minimal reproducible example.
### Example Code
```python
from dataclasses import dataclass
from collections.abc import Sequence
from langgraph._internal._serde import collect_allowlist_from_schemas
@dataclass
class Item:
x: int
@dataclass
class State:
items: Sequence[Item]
allowlist = collect_allowlist_from_schemas(schemas=[State])
print(allowlist)
```
### Error Message and Stack Trace
On Python 3.10, this raises:
```text
Traceback (most recent call last):
File "", line 17, in
File ".../langgraph/_internal/_serde.py", line 112, in collect_allowlist_from_schemas
_collect_from_type(schema, allowlist, seen, seen_ids)
File ".../langgraph/_internal/_serde.py", line 191, in _collect_from_type
_collect_from_type(field_type, allowlist, seen, seen_ids)
File ".../langgraph/_internal/_serde.py", line 174, in _collect_from_type
if _is_pydantic_model(typ):
File ".../langgraph/_internal/_serde.py", line 237, in _is_pydantic_model
return issubclass(typ, BaseModelV1)
File "/usr/lib/python3.10/abc.py", line 123, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class
```
### Description
`collect_allowlist_from_schemas()` recurses through built-in containers such as `list[T]`, `dict[K, V]`, and `deque[T]`, but it does not currently handle abstract containers from `collections.abc`, such as `Sequence[T]`, `Mapping[K, V]`, and `Iterable[T]`.
For `collections.abc.Sequence[Item]` on Python 3.10, `isinstance(typ, type)` returns `True`, but passing that object to `issubclass()` can raise `TypeError`. That means allowlist collection can fail before it has a chance to collect nested custom types.
I would expect abstract containers to be treated similarly to the supported built-in containers, so the example above should return an allowlist containing both `State` and `Item`.
### Proposed fix
I have a draft PR with a small regression test and fix here:
https://github.com/langchain-ai/langgraph/pull/7600
The change:
- recurses into common `collections.abc` generic containers
- guards pydantic model detection against `TypeError` from `issubclass()`
- extends `test_serde_allowlist.py` to cover `Sequence`, `Mapping`, and `Iterable`
### System Info
Reproduced against `main` at `45246f6c`.
```text
Python: 3.10.12
langgraph source checkout: langchain-ai/langgraph main
```
Contributor guide
Research direction
Start with langgraph/_internal/_serde.py, especially collect_allowlist_from_schemas and _collect_from_type, then review the regression coverage in test_serde_allowlist.py. Run the existing serde allowlist tests and confirm that Sequence, Mapping, and Iterable schemas collect nested custom types without a TypeError. A draft pull request already contains proposed work for this issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100