fix(anthropic): preserve streamed values conversion from main

This commit is contained in:
Joshua Valluru 2026-10-20 23:39:40 -07:00
parent e083b12f48
commit 8bf909dbe0
2 changed files with 43 additions and 0 deletions

View file

@ -116,6 +116,8 @@ def _prefix_verdict(buffered_json: str, schema: ResponseFormatSchema, tool_name:
return "hold"
if first_value is None and _NAME_VALUE_PREFIX.match(buffered_json):
return "undecided"
if first_key.group(1) == "values" and "values" not in schema.property_names:
return "hold"
if first_key.group(1) in schema.property_names or not schema.property_names or schema.accepts_a_lone_undeclared_key:
return "pass_through"
return _first_value_verdict(buffered_json[first_key.end() :], schema)

View file

@ -2742,3 +2742,44 @@ def test_json_envelope_split_before_its_value_keeps_the_schema_object() -> None:
split: Final = envelope.index(":") + 1
contents: Final = _streamed_json_mode_contents((envelope[:split], envelope[split:]))
assert json.loads("".join(contents)) == _STREAMED_RISK_OBJECT
@pytest.mark.parametrize(
"schema, values",
[
pytest.param(
{"type": "object", "additionalProperties": True}, {"alpha": 1, "beta": 2}, id="schema_without_properties"
),
pytest.param(
{
"type": "object",
"properties": {"summary": {"type": "string"}},
"additionalProperties": {"type": "object"},
},
{"user_1": {"summary": "likes tea"}},
id="undeclared_key_the_schema_allows",
),
pytest.param(_STREAMED_RISK_SCHEMA, [3, 5, 8], id="values_holding_a_list"),
],
)
def test_streamed_json_mode_values_wrapper_is_unwrapped_like_the_non_stream_answer(
schema: dict[str, object], values: object
):
wrapped = {"values": values}
encoded = json.dumps(wrapped)
contents = _streamed_json_mode_contents((encoded[:6], encoded[6:14], encoded[14:]), schema)
assert json.loads("".join(contents)) == values
assert json.loads("".join(contents)) == unwrap_response_format_envelope(
wrapped, response_format_schema(schema), RESPONSE_FORMAT_TOOL_NAME
)
def test_streamed_json_mode_declared_values_key_streams_as_it_arrives():
schema = {"type": "object", "properties": {"values": {"type": "array"}}, "required": ["values"]}
answer = json.dumps({"values": [3, 5, 8]})
contents = _streamed_json_mode_contents((answer[:6], answer[6:14], answer[14:]), schema)
assert "".join(contents) == answer
assert any(contents[:-2])