Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(chat): bound the provider error body written to the log

Bingxi Zhao (Frank)48ba9d405c6c
diff --git a/deeptutor/agents/chat/agent_loop.py b/deeptutor/agents/chat/agent_loop.py
index 868e04f10..bda1a3766 100644
--- a/deeptutor/agents/chat/agent_loop.py
+++ b/deeptutor/agents/chat/agent_loop.py
@@ -48,11 +48,11 @@ from deeptutor.services.llm import LLMProviderTransportError, clean_thinking_tag
from deeptutor.services.llm.capabilities import threads_session_id
from deeptutor.services.llm.multimodal import should_degrade_to_text, strip_image_parts_inplace
from deeptutor.services.llm.request_compat import (
- error_text,
is_image_input_unsupported,
is_stream_options_unsupported,
is_tool_schema_unsupported,
is_transient_transport_error,
+ logged_error_text,
)
if TYPE_CHECKING: # pragma: no cover
@@ -879,7 +879,7 @@ class AgentLoop:
logger.warning(
"provider rejected tool schemas for model=%s; retrying without tools. error=%s",
kwargs.get("model"),
- error_text(exc),
+ logged_error_text(exc),
)
await self.stream.progress(
self.pipeline._t(
diff --git a/deeptutor/services/llm/request_compat.py b/deeptutor/services/llm/request_compat.py
index 29550c788..2080518cd 100644
--- a/deeptutor/services/llm/request_compat.py
+++ b/deeptutor/services/llm/request_compat.py
@@ -25,6 +25,26 @@ def _exception_chain(exc: Exception):
pending.append(cause)
+_MAX_LOGGED_ERROR_CHARS = 2000
+
+
+def logged_error_text(exc: Exception) -> str:
+ """``error_text`` bounded for a log line.
+
+ The compat predicates only scan this text, but a log line is different:
+ ``data/user/logs/deeptutor.jsonl`` is what a user is asked to attach to a
+ bug report, and some providers echo the rejected request back — the tool
+ schemas, occasionally the messages themselves. The parameter a provider
+ objects to is always near the front, so cap it rather than ship an
+ unbounded copy of the request into a file destined for a public issue.
+ """
+ text = error_text(exc)
+ if len(text) <= _MAX_LOGGED_ERROR_CHARS:
+ return text
+ dropped = len(text) - _MAX_LOGGED_ERROR_CHARS
+ return f"{text[:_MAX_LOGGED_ERROR_CHARS]}… (+{dropped} chars)"
+
+
def error_text(exc: Exception) -> str:
"""Return the best available lowercase provider error body."""
response = getattr(exc, "response", None)
diff --git a/tests/services/llm/test_request_compat.py b/tests/services/llm/test_request_compat.py
index 05e2b3bab..d3b9a707e 100644
--- a/tests/services/llm/test_request_compat.py
+++ b/tests/services/llm/test_request_compat.py
@@ -52,3 +52,25 @@ def test_transient_transport_classifier_walks_wrapped_causes() -> None:
def test_transient_transport_classifier_excludes_provider_rejections() -> None:
assert not is_transient_transport_error(RuntimeError("401 invalid API key"))
assert not is_transient_transport_error(RuntimeError("429 rate limit exceeded"))
+
+
+def test_logged_error_text_is_bounded() -> None:
+ """A provider that echoes the request back must not fill the log with it.
+
+ The log line exists so a user can attach it to a bug report, so an
+ unbounded body would put the request — tool schemas, sometimes the
+ messages — into a file headed for a public issue.
+ """
+ from deeptutor.services.llm.request_compat import (
+ _MAX_LOGGED_ERROR_CHARS,
+ logged_error_text,
+ )
+
+ short = ValueError("unsupported parameter: tools")
+ assert logged_error_text(short) == "unsupported parameter: tools"
+
+ huge = ValueError("x" * (_MAX_LOGGED_ERROR_CHARS + 500))
+ bounded = logged_error_text(huge)
+ assert bounded.startswith("x" * 50)
+ assert len(bounded) < _MAX_LOGGED_ERROR_CHARS + 40
+ assert bounded.endswith("(+500 chars)")