Repository navigation
fix(analyzer): fall back to unforced tool calling when a gateway rejects json_schema - #1092
Conversation
…cts json_schema OpenAI-compatible gateways without JSON-schema response_format (DeepSeek) answer every LLM analyzer request with HTTP 400, while the report still says llm_available: true with risk 0 / LOW. The default binding sends response_format json_schema for any model with no registry entry, and the SKILLSPECTOR_STRUCTURED_OUTPUT_METHOD=function_calling override does not save it either: LangChain still forces one named tool call, which DeepSeek rejects the same way. On a structured-output rejection (HTTP 400 naming response_format or tool_choice), rebind the schema once as an unforced tool call (tool_choice left at auto, call requested in the prompt) and retry the batch. The fallback sticks for the analyzer instance, so later batches and formatting retries bind it directly instead of paying one rejected request each. When the gateway rejects the fallback too, the batch fails with the distinct reason llm_structured_output_rejected, and a run where every recorded call failed that way reports llm_available: false with the rejection surfaced in llm_error, instead of a clean-looking LOW-risk result. Fixes NVIDIA#1089 Signed-off-by: Varshith Puli <[email protected]>
yashrajp22
left a comment
There was a problem hiding this comment.
Source review for #1089 at 8716afb. I completed the independent Codex pass and adjudicated a completed Greptile review of the exact base/head comparison. The three source-confirmed issues below need correction. Runtime regression, source/wheel sample checks, and live-provider checks were not run in this source-only phase and remain pending.
…available error, deadline refresh before retry Snapshot whether each batch started on the fallback binding instead of reading the shared flag after its request completes, so a concurrent batch that flips the flag cannot make an in-flight request look already retried. The shared flag still guides future bindings. Reserve the structured-output unavailable error for total rejection: a successful or recovered batch keeps the coverage failure without the unavailable error, in llm_call_record_for_outcome and the TP4 aggregation. Refresh the remaining workflow deadline and request timeout before the fallback call in both sync and async paths, so an expired deadline sends no retry and a nearly exhausted scan does not wait out the old timeout again. Regression tests: two simultaneous initial rejections both recover; mixed success plus rejection keeps the coverage failure without the unavailable error (record helper and TP4); an expired deadline during the first rejection sends no fallback request. Signed-off-by: Varshith Puli <[email protected]>
yashrajp22
left a comment
There was a problem hiding this comment.
The request-local fallback state, mixed-result accounting, and deadline refresh are corrected. One error-classification issue remains: an unrelated fallback failure can inherit the first request’s rejection and produce a misleading structured-output-unavailable error.
…itial rejection The tool-calling fallback runs inside the handler for the initial json_schema rejection, so Python links that 400 as the retry error's implicit context. is_structured_output_rejection walks the chain, so an unrelated fallback failure (for example, an authentication error) was reported as structured output unavailable, hiding the real problem. Add a stop_before bound to the chain walk and pass the initial rejection at both fallback sites, sync and async, so only the fallback request's own failure can raise LLMStructuredOutputUnavailableError. Regression tests use a bare (uncaused) auth error, the shape where the walk follows __context__ to the stale 400, plus the loopback 401 shape for both paths. The new tests fail before this fix with the run reported as structured output unavailable. Signed-off-by: Varshith Puli <[email protected]>
CI (langchain-openai 1.3.3 from uv.lock) raises plain openai.AuthenticationError on the 401 fallback, while newer langchain-openai wraps it as OpenAIAuthenticationError. The production classifier already preserves the real error either way; the test expectation was pinned to one spelling. Signed-off-by: Varshith Puli <[email protected]>
|
CI on 4b2fc75 failed test-unit with 2 failures in test_structured_output_fallback.py (sync and async auth-failure tests). Root cause was in our own test expectation, not the fix: the test pinned error_class to "OpenAIAuthenticationError", which is the langchain-openai >= 1.4 wrapper name. CI installs langchain-openai 1.3.3 from uv.lock, where the same 401 surfaces as plain openai.AuthenticationError. The production classifier already preserves the real error either way; the two tests now accept both names. Fixed as 749954a, full fallback file green locally (23/23). |
rng1995
left a comment
There was a problem hiding this comment.
[SkillSpector Review]
Decision: Approved
Value and readiness
This addresses a meaningful scan-integrity and provider-compatibility failure: an endpoint rejecting the structured-output mechanism can recover using an unforced tool call, and actual total rejection is no longer presented as an available semantic scan. The current source and tests support approval; existing other-reviewer decisions/conversations remain merge gates.
Material findings
No required source correction identified after reading the full twelve-file change. The four reported human issues are corrected: each in-flight request snapshots its original binding so sibling rejections can each retry; successful or recovered batches prevent total-unavailability accounting; both fallback paths refresh the remaining deadline/model before sending another request; and exception classification stops before the initial rejection, preserving unrelated retry failures such as authentication errors. Later batches and formatting bind the cached fallback directly. The unforced binding still requires an actual validated tool response; parse/refusal/truncation handling remains at the shared boundary. The additive ledger reason and shared call-record helper connect total rejection to unavailable/degraded reporting while mixed outcomes retain their coverage failure.
PIC tradeoffs
The first mechanism-specific HTTP 400 can incur one extra bounded protocol attempt. Explicit method configuration is bypassed only for that recovery; the normal first binding is unchanged. No new dependency or live model service is introduced.
Verification and gaps
Read the complete production diff and all 619 lines of new test source, plus exact-head binding, invocation, deadline, serialization, validation and report context and every current review thread/reply. The loopback tests inspect wire fields, second-batch reuse, sync/async paths, simultaneous rejection, mixed/total results, expiry, bare/wrapped auth errors and full-graph CAUTION/unavailable metadata. All six exact-head hosted checks succeeded. The current API may name an auth error differently across installed LangChain versions; both accepted names retain the status/reason assertions. No live DeepSeek request or contributor test, application code, import, build, hook, scanner/model call or regex probe was executed here. Four unresolved human threads and the active external Changes Requested review still block merge.
Reviewed head: 749954a179faaec7b6648153630e699c5fb1ab81.
Fixes #1089
Problem
OpenAI-compatible gateways without JSON-schema response_format (DeepSeek) answer every LLM analyzer request with HTTP 400, while the report still says llm_available: true with risk 0 / LOW.
Two things combine to cause it:
Fix
Tests
New tests/unit/test_structured_output_fallback.py (14 tests) drives a loopback stub endpoint that 400s json_schema and forced tool calls and 200s unforced tool calls, covering: the rejection detector, the fallback wire shape (no response_format, no tool_choice), second-batch direct fallback binding, the async path, the exhausted-fallback reason and call-log error, and a full-graph scan reporting llm_available: false with llm_error naming the rejection.
Existing suites run green: test_llm_utils, test_tp4_openai_http, test_structured_response, test_model_capabilities, test_bedrock_provider (773 passed), test_report, test_semantic_runtime, test_inspection_ledger, test_provider_failure_coverage (465 passed), test_mcp_tool_poisoning, test_mcp_server (258 passed). ruff check and ruff format --check clean; mypy shows the same 25 pre-existing errors as clean main, none new.