feat(responses): enforce required and forced function choice

This commit is contained in:
Pavlo Ratushnyi
2026-10-04 09:51:39 +02:00
parent fe2d3113d4
commit 413b6988fd
5 changed files with 225 additions and 24 deletions
+34 -7
View File
@@ -72,6 +72,21 @@ done
('none', True, custom_call, 1, False),
('none', False, both, 2, False),
]
forced = {'type': 'function', 'name': 'lookup'}
cases.extend([
('required', True, 'OK', 0, False),
('required', True, function_call, 1, True),
('required', True, custom_call, 1, True),
('required', True, both, 2, True),
('required', False, both, 2, False),
(forced, True, function_call, 1, True),
(forced, True, custom_call, 1, False),
(forced, True, both, 2, False),
(forced, True, function_call + '\n' + function_call, 2, False),
(forced, True, 'OK', 0, False),
])
cases = [(*case, 'stop') for case in cases]
cases.extend((choice, True, 'OK', 0, True, 'length') for choice in ['required', forced])
with tempfile.TemporaryFile() as logs:
process = subprocess.Popen([binary, 'serve'], env=env, stdout=logs, stderr=logs, start_new_session=True)
try:
@@ -87,9 +102,9 @@ done
total = 0
for effort in ['none', 'low']:
for stream in [False, True]:
for choice, parallel, output, count, valid in cases:
for choice, parallel, output, count, valid, reason in cases:
data = (('Think.</think>\n\n' if effort == 'low' else '') + output).encode()
(root / 'next').write_text(''.join(f'T {byte}\n' for byte in data) + f'DONE {len(data)} 7 1.0 2.0 stop\n')
(root / 'next').write_text(''.join(f'T {byte}\n' for byte in data) + f'DONE {len(data)} 7 1.0 2.0 {reason}\n')
payload = {'model': 'fixture', 'input': 'Use policy', 'max_output_tokens': 1024,
'reasoning': {'effort': effort}, 'stream': stream, 'tools': tools,
'metadata': {'fixture': 'policy'}}
@@ -103,11 +118,11 @@ done
assert code == 200, (code, body)
events = [json.loads(line[5:]) for line in body.splitlines() if line.startswith('data:')]
assert [e['sequence_number'] for e in events] == list(range(len(events)))
assert events[-1]['type'] == ('response.completed' if valid else 'response.failed'), body
assert events[-1]['type'] == (('response.incomplete' if reason == 'length' else 'response.completed') if valid else 'response.failed'), body
published = [e['item'] for e in events if e['type'] == 'response.output_item.added'
and e['item']['type'] in ('function_call', 'custom_tool_call')]
if not valid:
allowed = 1 if choice == 'auto' and effort == 'none' else 0
allowed = 1 if choice != 'none' and effort == 'none' and output.startswith(function_call) else 0
assert len(published) == allowed, body
assert not any('custom_tool_call_input' in e['type'] for e in events), body
continue
@@ -119,6 +134,7 @@ done
continue
assert code == 200, (code, body)
result = json.loads(body)
assert result['status'] == ('incomplete' if reason == 'length' else 'completed')
assert result['tool_choice'] == (choice or 'auto')
assert result['parallel_tool_calls'] == (parallel if parallel is not None else True)
assert result['tools'] == tools and result['metadata'] == {'fixture': 'policy'}
@@ -131,10 +147,21 @@ done
assert json.loads(call['arguments']) == {'key': 'value'}
if count == 0:
assert result['output'][-1]['content'][0]['text'] == 'OK'
for unsupported in ['required', {'type': 'function', 'name': 'lookup'}]:
for invalid in [
{'tool_choice': 'required', 'tools': []},
{'tool_choice': 'required', 'tools': [{'type': 'namespace', 'name': 'empty', 'tools': []}]},
{'tool_choice': {'type': 'function'}},
{'tool_choice': {'type': 'function', 'name': 'missing'}},
{'tool_choice': {'type': 'function', 'name': alias}},
{'tool_choice': {'type': 'function', 'name': 1}},
{'tool_choice': {**forced, 'namespace': 'code'}},
{'tool_choice': forced, 'tools': [{'type': 'custom', 'name': 'lookup'}]},
]:
assert request('/v1/responses', {**payload, **invalid})[0] == 400, invalid
for unsupported in [{'type': 'allowed_tools', 'mode': 'auto', 'tools': []}, {'type': 'custom', 'name': 'run'}]:
payload['tool_choice'] = unsupported
assert request('/v1/responses', payload)[0] == 501
assert json.loads(request('/metrics')[1])['generation']['started'] == total == 36
assert json.loads(request('/metrics')[1])['generation']['started'] == total == 84
finally:
process.terminate()
try:
@@ -142,7 +169,7 @@ done
except subprocess.TimeoutExpired:
os.killpg(process.pid, signal.SIGKILL)
process.wait()
print('Tool policy: 36 CPU HTTP cases passed; none, single-call, auto/default, mixed tools, reasoning, JSON/SSE, metadata and exact accepted input')
print('Tool policy: 84 CPU HTTP cases passed; auto/none/required/forced, single-call, incomplete, mixed tools, reasoning, JSON/SSE, 8 malformed choices, metadata and exact accepted input')
if __name__ == '__main__':