Skip to content

Commit

Permalink
modified: libs/core/langchain_core/output_parsers/base.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahonanhin committed Dec 3, 2024
1 parent a323b70 commit 4eed603
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions libs/core/langchain_core/output_parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,13 @@ def update_and_check_repetition(self, output_text: str) -> bool:
"""
reasoning_history = getattr(self, "_reasoning_history", [])
# Check for repetition (if the last two outputs match the current one)
if len(reasoning_history) > 1 and reasoning_history[-1] == reasoning_history[-2] == output_text:
if len(reasoning_history) > 1 and \
reasoning_history[-1] == reasoning_history[-2] == output_text:
return True

# Update the reasoning history (keep only the last 3 items)
reasoning_history.append(output_text)
setattr(self, "_reasoning_history", reasoning_history[-self._history_size:])
self._reasoning_history = reasoning_history[-self._history_size:]

return False

Expand Down Expand Up @@ -275,11 +276,13 @@ def parse_result(self, result: list[Generation], *, partial: bool = False) -> T:
output_text = result[0].text.strip()

if self.update_and_check_repetition(output_text):
logger.warning("Detected repetitive reasoning or circular logic: %s", output_text)
raise ValueError("Detected repetitive reasoning or circular logic. Terminating.")
raise ValueError("Detected repetitive reasoning \
or circular logic. Terminating.")

Check failure on line 280 in libs/core/langchain_core/output_parsers/base.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.12

Ruff (EM101)

langchain_core/output_parsers/base.py:279:30: EM101 Exception must not use a string literal, assign to variable first

Check failure on line 280 in libs/core/langchain_core/output_parsers/base.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (EM101)

langchain_core/output_parsers/base.py:279:30: EM101 Exception must not use a string literal, assign to variable first

Check failure on line 280 in libs/core/langchain_core/output_parsers/base.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.9

Ruff (EM101)

langchain_core/output_parsers/base.py:279:30: EM101 Exception must not use a string literal, assign to variable first

Check failure on line 280 in libs/core/langchain_core/output_parsers/base.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.10

Ruff (EM101)

langchain_core/output_parsers/base.py:279:30: EM101 Exception must not use a string literal, assign to variable first

Check failure on line 280 in libs/core/langchain_core/output_parsers/base.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.13

Ruff (EM101)

langchain_core/output_parsers/base.py:279:30: EM101 Exception must not use a string literal, assign to variable first

if "iteration limit exceeded" in output_text.lower() or "unable to proceed" in output_text.lower():
return {"error": "Agent terminated due to iteration limit or inability to continue."}
if "iteration limit exceeded" in output_text.lower()\
or"unable to proceed" in output_text.lower():
return {"error": "Agent terminated due to iteration\
limit or inability to continue."}

try:
return output_text.strip()
Expand Down

0 comments on commit 4eed603

Please sign in to comment.