feat: continue Messages with failed or empty tool results (#27)
This commit is contained in:
@@ -247,6 +247,48 @@ done
|
||||
assert measured == len(expected_wire.split()[-1].split(','))
|
||||
code, result = request('POST', path, {**base, 'messages': transcript, 'tools': [tool]})
|
||||
assert code == 200 and wire()[-1] == expected_wire and result['content'] == [{'type': 'text', 'text': 'Continued'}], result
|
||||
# Tool execution failures and empty results continue in the tool role.
|
||||
error_text = 'Connection failed 🌍\n"quoted" \\ path\u0000'
|
||||
variants = [
|
||||
([{'content': [{'type': 'text', 'text': error_text}], 'is_error': True}, {}],
|
||||
[json.dumps({'content': error_text, 'is_error': True}, ensure_ascii=False, separators=(',', ':'), sort_keys=True), '']),
|
||||
([{'is_error': True}, {'content': [], 'is_error': False}],
|
||||
['{"content":"","is_error":true}', '']),
|
||||
([{'content': 'Found 🌍', 'is_error': False}, {'content': 'Found 🌍'}],
|
||||
['Found 🌍', 'Found 🌍']),
|
||||
]
|
||||
for fields, expected_outputs in variants:
|
||||
results = [{'type': 'tool_result', 'tool_use_id': b['id'], **f} for b, f in zip(calls, fields)]
|
||||
transcript = base['messages'] + [{'role': 'assistant', 'content': streamed['content']}, {'role': 'user', 'content': results}]
|
||||
current = {**base, 'messages': transcript, 'tools': [tool]}
|
||||
native_input = base['messages'] + [{'type': 'function_call', 'call_id': b['id'], 'name': b['name'], 'arguments': json.dumps(b['input'], separators=(',', ':'))} for b in calls] + [{'type': 'function_call_output', 'call_id': b['id'], 'output': v} for b, v in zip(calls, expected_outputs)]
|
||||
measured = count(current)
|
||||
output('Continued after result')
|
||||
assert request('POST', '/v1/responses', {**native_base, 'input': native_input, 'tools': [native_tool]})[0] == 200
|
||||
expected_wire = wire()[-1]
|
||||
assert measured == len(expected_wire.split()[-1].split(','))
|
||||
code, result = request('POST', path, current)
|
||||
assert code == 200 and wire()[-1] == expected_wire and result['stop_reason'] == 'end_turn', result
|
||||
assert result['content'] == [{'type': 'text', 'text': 'Continued after result'}]
|
||||
code, events = request('POST', path, {**current, 'stream': True})
|
||||
assert code == 200 and wire()[-1] == expected_wire
|
||||
assert normalized(rebuild(events)) == normalized(result)
|
||||
before = wire()
|
||||
for bad in [
|
||||
{'content': None, 'is_error': True}, {'is_error': 'true'},
|
||||
{'content': [{'type': 'image', 'source': {}}], 'is_error': True},
|
||||
{'tool_use_id': 'orphan', 'is_error': True},
|
||||
{'tool_use_id': calls[1]['id'], 'is_error': True},
|
||||
]:
|
||||
bad_results = [{'type': 'tool_result', 'tool_use_id': calls[0]['id'], **bad},
|
||||
{'type': 'tool_result', 'tool_use_id': calls[1]['id']}]
|
||||
current = {**base, 'tools': [tool], 'messages': base['messages'] + [
|
||||
{'role': 'assistant', 'content': streamed['content']}, {'role': 'user', 'content': bad_results}]}
|
||||
for streaming in [False, True]:
|
||||
code, value = request('POST', path, {**current, 'stream': streaming})
|
||||
assert code == 400 and value['type'] == 'error', value
|
||||
count(current, 400)
|
||||
assert wire() == before
|
||||
for text, extra, reason in [(tool_text.replace('lookup', 'wrong'), {'tools': [tool]}, 'stop'), ('A', {'tools': [tool], 'tool_choice': {'type': 'any'}}, 'stop'), (tool_text[:35], {'tools': [tool]}, 'length')]:
|
||||
output(text, reason)
|
||||
code, value = request('POST', path, {**base, **extra})
|
||||
@@ -310,7 +352,7 @@ done
|
||||
if process.returncode != 0:
|
||||
logs.seek(0)
|
||||
raise AssertionError(logs.read().decode())
|
||||
print('Messages count/actual GEN equality/no mutation/outage + JSON/named live SSE/block order/tools/continuation/usage/header auth/errors/STOP/fresh: PASS; actual GEN and greedy parity tolerance=0, CPU only')
|
||||
print('Messages error/empty/parallel tool results + count/actual GEN equality/no mutation/outage + JSON/named live SSE/block order/tools/continuation/usage/header auth/errors/STOP/fresh: PASS; actual GEN and greedy parity tolerance=0, CPU only')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user