rest: coerce non-string JSON response values to text#1910
Open
cognis-digital wants to merge 1 commit into
Open
rest: coerce non-string JSON response values to text#1910cognis-digital wants to merge 1 commit into
cognis-digital wants to merge 1 commit into
Conversation
A response_json_field / JSONPath can resolve to a dict, list, number, or bool rather than a string (seen with some Azure OpenAI response shapes). The value flowed unmodified into Message(), and a downstream detector then raised 'AttributeError: dict object has no attribute lower' after every subtest had run. Coerce non-string extracted values to text (dict/list -> JSON, scalar -> str) via _coerce_response_text, and keep single-match dict values instead of silently dropping them to [None]. Adds regression tests for the dict-valued field and JSONPath cases.
jmartin-tech
reviewed
Jul 6, 2026
jmartin-tech
left a comment
Collaborator
There was a problem hiding this comment.
How does this offering compare with #1892, I see possible tradeoffs to each.
| if value is None or isinstance(value, str): | ||
| return value | ||
| if isinstance(value, (dict, list)): | ||
| return json.dumps(value, ensure_ascii=False) |
Collaborator
There was a problem hiding this comment.
This could raise an exception that will not be handled well by the run if the content is not valid json.
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 #1888. A
response_json_field/ JSONPath can resolve to a dict, list, number, or bool rather than a string — observed with some Azure OpenAI REST response shapes. The extracted value flowed unmodified intoMessage(), and a downstream detector then raisedAttributeError: 'dict' object has no attribute 'lower'— only after every subtest in the probe had run, so the whole run was wasted.Fix
_coerce_response_text(): passes strings/None through, serializes dict/list to JSON, andstr()s other scalars — so the value handed toMessage()is always text.response = [None](which discarded a real response).Tests
Adds
test_json_rest_dict_value_is_coercedandtest_json_rest_jsonpath_dict_value_is_coercedmirroring the existingrequests_mockREST tests: a response field that resolves to a dict now yields a stringMessage(what detectors call.lower()on) instead of crashing.Minimal and backwards-compatible: string and list responses are unchanged.