Add switchable stateless Responses API compatibility

This commit is contained in:
Mikei386 committed 2026-10-04 10:38:10 +02:00
1 parent a908313c8f
commit 650eb398c6
11 files changed
+376 -18

No files matched your search

+26 -1
View File
@@ -28,7 +28,7 @@ class Upstream(BaseHTTPRequestHandler):
def do_POST(self):
w=self.server.worker;data=json.loads(self.rfile.read(int(self.headers['Content-Length'])));w.requests.append(data)
if data.get('stream'):
self.send_response(200);self.send_header('Content-Type','text/event-stream');self.end_headers();self.wfile.write(b'data: {"choices":[]}\n\n');self.wfile.flush();w.block.wait(3);self.wfile.write(b'data: [DONE]\n\n')
self.send_response(200);self.send_header('Content-Type','text/event-stream');self.end_headers();self.wfile.write(getattr(w,'stream_frame',b'data: {"choices":[]}\n\n'));self.wfile.flush();w.block.wait(3);self.wfile.write(b'data: [DONE]\n\n')
else:
body=json.dumps(dict(id='test',model=data['model'],choices=[{'message':{'role':'assistant','content':'synthetic'}}])).encode();self.send_response(200);self.send_header('Content-Length',str(len(body)));self.end_headers();self.wfile.write(body)
@@ -70,6 +70,31 @@ class EndpointTests(unittest.TestCase):
for name in ['qwen-medium','Qwen Medium','Old Name']:
self.assertEqual(self.ep.find_profile(name,'chat')['id'],'alpha')
def test_responses_toggle_auth_and_existing_route(self):
self.enable('alpha')
self.assertEqual(self.request('/v1/responses',dict(model='alpha',input='hi'))[0],404)
self.ep.configure({'responses_compat':True})
self.assertTrue(self.ep.status()['responses_compat'])
self.assertEqual(self.request('/v1/responses',dict(model='alpha',input='hi'),token='bad')[0],401)
status,result=self.request('/v1/responses',dict(model='alpha',input=[dict(role='user',content='hi'),dict(role='developer',content='late instruction')]))
self.assertEqual(status,200);self.assertEqual(result['object'],'response');self.assertEqual(result['output'][0]['content'][0]['text'],'synthetic')
self.assertEqual(self.worker.requests[-1]['messages'][0],dict(role='system',content='late instruction'))
self.assertNotIn('/v1/responses',self.request('/v1/capabilities')[1]['unsupported_routes'])
saved=json.loads((self.ep.root/'endpoint.json').read_text());self.assertTrue(saved['responses_compat'])
self.ep.configure({'responses_compat':False})
self.assertEqual(self.request('/v1/responses',dict(model='alpha',input='hi'))[0],404)
self.assertEqual(self.request('/v1/chat/completions',dict(model='alpha',messages=[dict(role='user',content='hi')]))[0],200)
with self.assertRaises(ValueError):self.ep.configure({'responses_compat':'true'})
def test_responses_successful_stream_and_failed_partial_stream(self):
self.enable('alpha');self.ep.configure({'responses_compat':True})
self.worker.stream_frame=b'data: {"choices":[{"delta":{"content":"hello"},"finish_reason":"stop"}]}\n\n'
status,body=self.request('/v1/responses',dict(model='alpha',input='hi',stream=True))
self.assertEqual(status,200);self.assertIn(b'response.output_text.delta',body);self.assertIn(b'response.completed',body)
self.worker.stream_frame=b'data: {"choices":[{"delta":{"content":"partial"}}]}\n\n'
_,body=self.request('/v1/responses',dict(model='alpha',input='hi',stream=True))
self.assertIn(b'response.failed',body);self.assertNotIn(b'response.completed',body)
def test_discovery_reports_profile_context_without_loading(self):
self.rows[0]['parameters'].update(context=160000,slots=2)
self.rows[0]['model']={'repo':'Qwen/Qwen3.8-27B'}