core[patch]: fixed circular dependency with json schema (#18657)

**Description:** Circular dependencies when parsing references leading
to `RecursionError: maximum recursion depth exceeded` issue. This PR
address the issue by handling previously seen refs as in any typical DFS
to avoid infinite depths.

**Issue:** https://github.com/langchain-ai/langchain/issues/12163

 **Twitter handle:** https://twitter.com/theBhulawat 


- [x] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
This commit is contained in:
Naman Jain
2024-03-12 11:12:45 +05:30
committed by GitHub
parent 0bec1f6877
commit 75122646b5
2 changed files with 86 additions and 9 deletions

View File

@@ -181,3 +181,54 @@ def test_dereference_refs_integer_ref() -> None:
}
actual = dereference_refs(schema)
assert actual == expected
def test_dereference_refs_cyclical_refs() -> None:
schema = {
"type": "object",
"properties": {
"user": {"$ref": "#/$defs/user"},
"customer": {"$ref": "#/$defs/user"},
},
"$defs": {
"user": {
"type": "object",
"properties": {
"friends": {"type": "array", "items": {"$ref": "#/$defs/user"}}
},
}
},
}
expected = {
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"friends": {
"type": "array",
"items": {}, # Recursion is broken here
}
},
},
"customer": {
"type": "object",
"properties": {
"friends": {
"type": "array",
"items": {}, # Recursion is broken here
}
},
},
},
"$defs": {
"user": {
"type": "object",
"properties": {
"friends": {"type": "array", "items": {"$ref": "#/$defs/user"}}
},
}
},
}
actual = dereference_refs(schema)
assert actual == expected