diff --git a/libs/core/langchain_core/utils/json_schema.py b/libs/core/langchain_core/utils/json_schema.py index 3368f83813..be5c8e25ba 100644 --- a/libs/core/langchain_core/utils/json_schema.py +++ b/libs/core/langchain_core/utils/json_schema.py @@ -13,10 +13,12 @@ def _retrieve_ref(path: str, schema: dict) -> dict: ) out = schema for component in components[1:]: - if component.isdigit(): + if component in out: + out = out[component] + elif component.isdigit() and int(component) in out: out = out[int(component)] else: - out = out[component] + raise KeyError(f"Reference '{path}' not found.") return deepcopy(out) diff --git a/libs/core/tests/unit_tests/utils/test_json_schema.py b/libs/core/tests/unit_tests/utils/test_json_schema.py index 44ad61609b..cb2add7476 100644 --- a/libs/core/tests/unit_tests/utils/test_json_schema.py +++ b/libs/core/tests/unit_tests/utils/test_json_schema.py @@ -183,6 +183,38 @@ def test_dereference_refs_integer_ref() -> None: assert actual == expected +def test_dereference_refs_string_ref() -> None: + schema = { + "type": "object", + "properties": { + "error_400": {"$ref": "#/$defs/400"}, + }, + "$defs": { + "400": { + "type": "object", + "properties": {"description": "Bad Request"}, + }, + }, + } + expected = { + "type": "object", + "properties": { + "error_400": { + "type": "object", + "properties": {"description": "Bad Request"}, + }, + }, + "$defs": { + "400": { + "type": "object", + "properties": {"description": "Bad Request"}, + }, + }, + } + actual = dereference_refs(schema) + assert actual == expected + + def test_dereference_refs_cyclical_refs() -> None: schema = { "type": "object",