fix(api): replace bare except with specific exceptions in CotAgentOutputParser#38235
Open
xiaobao-k8s wants to merge 1 commit into
Open
fix(api): replace bare except with specific exceptions in CotAgentOutputParser#38235xiaobao-k8s wants to merge 1 commit into
xiaobao-k8s wants to merge 1 commit into
Conversation
…putParser Replace bare in with specific exception handling for , , and . Add to surface parse failures instead of silently swallowing them. This also prevents the bare except from catching and . Fixes langgenius#38085
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #38085 — replaces the bare
except:inCotAgentOutputParser.extra_json_from_code_block()with specific exception types (json.JSONDecodeError,re.error,ValueError) and adds alogger.warning()call so parse failures are visible in logs instead of being silently swallowed.Root cause: The bare
except:on line 52 ofapi/core/agent/output_parser/cot_output_parser.pycatches all exceptions includingKeyboardInterruptandSystemExit, and discards them without any log output. When an LLM returns a malformed JSON code block, the agent silently produces no action with no trace of the failure.Fix: Replace bare
except:withexcept (json.JSONDecodeError, re.error, ValueError) as eand log a warning.KeyboardInterruptandSystemExitnow propagate normally.Changes
api/core/agent/output_parser/cot_output_parser.py: +5 / -1 linesimport loggingand module-level loggerexcept:with specific exception types + warning logScreenshots
N/A (backend-only fix, no UI change)
Real behavior proof
Behavior or issue addressed: After the fix, malformed JSON code blocks from LLM responses are caught by specific exception handlers and logged at WARNING level instead of being silently swallowed.
KeyboardInterruptandSystemExitpropagate normally.Real environment tested: Repository
langgenius/dify, branchfix/bare-except-cot-output-parser-38085, commit18eb15000, using Python 3.12 on Linux x86_64.Exact steps or command run after this patch:
Evidence after fix: Terminal output:
Observed result after fix:
json.JSONDecodeError, logged at WARNING, and returns[][]as beforeKeyboardInterruptandSystemExitare no longer caught hereWhat was not tested:
Checklist
ruff check(backend) to appease the lint gods