feat: engine 透传 error_code(与 error 同源取最后一次异常)

This commit is contained in:
lhl
2026-08-09 13:48:00 +08:00
parent 3b98412492
commit aa9c63df73
3 changed files with 67 additions and 3 deletions
+7 -3
View File
@@ -101,6 +101,7 @@ class InferenceEngine:
start = time.monotonic()
last_error: str | None = None
last_error_code: str | None = None
for idx, name in enumerate(self._model_names(model)):
try:
text, usage = self._call(
@@ -115,12 +116,13 @@ class InferenceEngine:
)
except LLMError as exc:
last_error = str(exc)
last_error_code = exc.error_code # 同源:取最后一次失败异常
return ChatResult(
text="", model=name,
prompt_version=getattr(prompt, "version", "inline"),
usage=TokenUsage(), duration_ms=int((time.monotonic() - start) * 1000),
status="failed", error=last_error,
status="failed", error=last_error, error_code=last_error_code,
)
def chat_structured(
@@ -144,6 +146,7 @@ class InferenceEngine:
attempts = 0
last_raw = ""
last_error: str | None = None
last_error_code: str | None = None
while attempts <= retry_count:
attempts += 1
@@ -166,6 +169,7 @@ class InferenceEngine:
)
except json.JSONDecodeError as exc:
last_error = f"JSON 解析失败: {exc}"
last_error_code = "LLM_PARSE_ERROR"
# 带错误信息重试
base_rendered = base_rendered + f"\n\n上次解析失败:{exc}。请重新输出合法 JSON。"
except LLMError as exc:
@@ -175,7 +179,7 @@ class InferenceEngine:
prompt_version=getattr(prompt, "version", "inline"),
usage=TokenUsage(),
duration_ms=int((time.monotonic() - start) * 1000),
status="failed", error=str(exc),
status="failed", error=str(exc), error_code=exc.error_code,
)
return StructuredResult(
@@ -184,5 +188,5 @@ class InferenceEngine:
prompt_version=getattr(prompt, "version", "inline"),
usage=TokenUsage(),
duration_ms=int((time.monotonic() - start) * 1000),
status="parse_error", error=last_error,
status="parse_error", error=last_error, error_code=last_error_code,
)