Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libs/core/langchain_core/output_parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ async def aparse_result(
The return value is parsed from only the first `Generation` in the result, which
is assumed to be the highest-likelihood `Generation`.

A custom `aparse` is used only when `parse_result` is not overridden.

Args:
result: A list of `Generation` to be parsed.

Expand All @@ -298,6 +300,11 @@ async def aparse_result(
Returns:
Structured output.
"""
if (
type(self).aparse is not BaseOutputParser.aparse
and type(self).parse_result is BaseOutputParser.parse_result
):
return await self.aparse(result[0].text)
Comment on lines +303 to +307

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Custom aparse delegating to aparse_result now recurses forever

Some subclasses override aparse only to forward to aparse_result, e.g. async def aparse(self, text): return await self.aparse_result([Generation(text=text)]), and inherit parse_result. Before this change that worked, because aparse_result called parse_result → parse in an executor. Now aparse_result sees the overridden aparse and calls it, which calls aparse_result again. The result is RecursionError on ainvoke/aparse.

This is a less common pattern, but it is a silent behavior change for public subclasses. Please call it out in the release note, or add a test documenting that it isn't supported.

(Refers to lines 303-307)


Your feedback helps Robin learn. React with 👍 or 👎 to tell us if this review comment was useful.

return await run_in_executor(None, self.parse_result, result, partial=partial)

async def aparse(self, text: str) -> T:
Expand Down
77 changes: 77 additions & 0 deletions libs/core/tests/unit_tests/output_parsers/test_base_parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Module to test base parser implementations."""

import asyncio
import threading
from typing import Any, get_type_hints

import pytest
Expand Down Expand Up @@ -33,6 +35,81 @@ def test_base_output_parser_type_hints_resolve() -> None:
assert get_type_hints(BaseOutputParser.asdict)["return"] == dict[str, Any]


@pytest.mark.parametrize("inherited", [False, True])
@pytest.mark.parametrize("input", ["text", AIMessage(content="text")])
async def test_base_output_parser_ainvoke_uses_aparse(
*, inherited: bool, input: str | AIMessage
) -> None:
caller_loop = asyncio.get_running_loop()
caller_thread = threading.get_ident()

class AsyncParser(BaseOutputParser[str]):
def parse(self, text: str) -> str:
return asyncio.run(self.aparse(text))

async def aparse(self, text: str) -> str:
assert asyncio.get_running_loop() is caller_loop
assert threading.get_ident() == caller_thread
return text.upper()

class InheritedParser(AsyncParser):
pass

parser = InheritedParser() if inherited else AsyncParser()
assert await parser.ainvoke(input) == "TEXT"
assert await parser.aparse_result([Generation(text="text")]) == "TEXT"


@pytest.mark.parametrize("override_aparse", [False, True])
@pytest.mark.parametrize("partial", [False, True])
async def test_base_output_parser_preserves_parse_result(
*, override_aparse: bool, partial: bool
) -> None:
caller_thread = threading.get_ident()
message = AIMessage(
content="", tool_calls=[{"name": "tool", "args": {}, "id": "call_1"}]
)

class ResultParser(BaseOutputParser[str]):
def parse(self, text: str) -> str:
pytest.fail(f"parse must not be called with {text!r}")

def parse_result(
self, result: list[Generation], *, partial: bool = False
) -> str:
assert threading.get_ident() != caller_thread
assert isinstance(result[0], ChatGeneration)
assert result[0].message is message
return f"result:{partial}"

class AsyncResultParser(ResultParser):
async def aparse(self, text: str) -> str:
pytest.fail(f"aparse must not bypass parse_result for {text!r}")

class InheritedParser(AsyncResultParser):
pass

parser = InheritedParser() if override_aparse else ResultParser()
assert (
await parser.aparse_result([ChatGeneration(message=message)], partial=partial)
== f"result:{partial}"
)
assert await parser.ainvoke(message) == "result:False"


async def test_base_output_parser_keeps_sync_fallback() -> None:
caller_thread = threading.get_ident()

class SyncParser(BaseOutputParser[str]):
def parse(self, text: str) -> str:
assert threading.get_ident() != caller_thread
return text.upper()

parser = SyncParser()
assert await parser.ainvoke("text") == "TEXT"
assert await parser.aparse("text") == "TEXT"


def test_base_generation_parser() -> None:
"""Test Base Generation Output Parser."""

Expand Down
Loading