feat: add retryOnParsingError method to BaseAgent for enhanced error handling

This commit is contained in:
extio-de
2026-06-01 22:28:30 +02:00
parent 2083d4f33f
commit e1a70f5397
2 changed files with 100 additions and 85 deletions

View File

@@ -870,6 +870,7 @@ final var blogPostAgent = new Agent(
| `postProcess(context)` | Runs after completion. | no-op | | `postProcess(context)` | Runs after completion. | no-op |
| `merge(contexts)` | Merges split contexts after branching. | `null` | | `merge(contexts)` | Merges split contexts after branching. | `null` |
| `chooseNextAgent(context)` | Selects the next agent or ends the flow. Required. | none | | `chooseNextAgent(context)` | Selects the next agent or ends the flow. Required. | none |
| `retryOnParsingError(context)` | When `true`, triggers an outer retry loop that resets both parse attempts and tool call rounds after a parsing failure, allowing the agent to request a fresh completion from scratch. The conversation can be modified before retrying. | `false` |
### Agent Tool Calling ### Agent Tool Calling

View File

@@ -112,6 +112,10 @@ public interface BaseAgent {
return AgentNext.END; return AgentNext.END;
} }
default boolean retryOnParsingError(final AgentContext context) {
return false;
}
default List<AgentContext> execute(final Client client, final ExecutorService agentExecutorService, final AgentContext context) { default List<AgentContext> execute(final Client client, final ExecutorService agentExecutorService, final AgentContext context) {
final var splits = new ArrayList<Split>(); final var splits = new ArrayList<Split>();
@@ -159,10 +163,13 @@ public interface BaseAgent {
var toolCallData = supportsToolCalling ? this.toolCallData(split.context()) : null; var toolCallData = supportsToolCalling ? this.toolCallData(split.context()) : null;
this.rememberToolCallData(split.context(), toolCallData); this.rememberToolCallData(split.context(), toolCallData);
boolean retryOuterLoop;
boolean parseable = false; boolean parseable = false;
Exception parseException = null; Exception parseException = null;
int parseAttempts = 0; int parseAttempts = 0;
int toolCallRounds = 0; int toolCallRounds = 0;
do {
retryOuterLoop = false;
while (parseAttempts < 2) { while (parseAttempts < 2) {
LOGGER.debug("Conversation: {}", conversation); LOGGER.debug("Conversation: {}", conversation);
@@ -261,11 +268,18 @@ public interface BaseAgent {
} }
} }
if (!parseable) { if (!parseable) {
if (this.retryOnParsingError(split.context())) {
parseAttempts = 0;
toolCallRounds = 0;
retryOuterLoop = true;
continue;
}
LOGGER.warn("{} Response is still not parseable after last attempt", this.name()); LOGGER.warn("{} Response is still not parseable after last attempt", this.name());
split.context().getGraph().add("☢"); split.context().getGraph().add("☢");
split.context().setError(AgentErrorType.PARSING, parseException); split.context().setError(AgentErrorType.PARSING, parseException);
return; return;
} }
} while (retryOuterLoop);
} }
this.postProcess(split.context()); this.postProcess(split.context());