#!/usr/bin/env python3 """Kingy local response-parser exercise. Python standard library; no network/API. Run: python3 parser-fixture-test.py > parser-fixture-results.json Fixtures below are handwritten, non-sensitive format examples, not API captures. The typed parser is an illustration, not a production client or a migration SDK. """ import json from datetime import datetime, timezone def legacy_parser(response): return response['choices'][0]['text'] def typed_parser(response): output = response.get('output', []) text = ''.join(block['text'] for item in output if item.get('type') == 'message' for block in item.get('content', []) if block.get('type') == 'output_text') if any(item.get('type') == 'function_call' for item in output): state = 'tool_handling_required' elif response.get('status') != 'completed': state = 'incomplete' else: state = 'completed' return {'text': text, 'state': state} fixtures = { 'legacy_completion': {'choices': [{'text': 'Fixture answer.'}]}, 'responses_reasoning_then_text': { 'status': 'completed', 'output': [ {'type': 'reasoning', 'summary': []}, {'type': 'message', 'content': [ {'type': 'output_text', 'text': 'Fixture answer.'}]}]}, 'responses_incomplete': { 'status': 'incomplete', 'incomplete_details': {'reason': 'max_output_tokens'}, 'output': [{'type': 'message', 'content': [ {'type': 'output_text', 'text': 'Partial fixture'}]}]}, 'responses_tools_only': { 'status': 'completed', 'output': [ {'type': 'function_call', 'call_id': 'fixture_call', 'name': 'lookup_fixture', 'arguments': '{"id":"example"}'}]}, } expected = { 'responses_reasoning_then_text': {'text': 'Fixture answer.', 'state': 'completed'}, 'responses_incomplete': {'text': 'Partial fixture', 'state': 'incomplete'}, 'responses_tools_only': {'text': '', 'state': 'tool_handling_required'}, } results = [] for name, fixture in fixtures.items(): try: old = {'result': legacy_parser(fixture), 'error': None} except (KeyError, IndexError, TypeError) as exc: old = {'result': None, 'error': type(exc).__name__} if name == 'legacy_completion': assert old['result'] == 'Fixture answer.' new = None else: assert old['error'] == 'KeyError' new = typed_parser(fixture) assert new == expected[name] results.append({'fixture': name, 'input': fixture, 'legacy_parser': old, 'typed_parser': new}) print(json.dumps({ 'test_name': 'Local legacy-Completions to Responses parser fixtures', 'executed_at_utc': datetime.now(timezone.utc).isoformat(), 'fixture_origin': 'Handwritten synthetic format examples; not live API responses', 'network_requests': 0, 'api_calls': 0, 'spend_usd': 0, 'result': 'Format handling assertions passed; complete API migration untested', 'legacy_parser': 'Passed legacy fixture; failed all three Responses fixtures as expected', 'typed_parser': 'Handled all three Responses fixture states as expected', 'partial_result': 'Tools-only fixture still needs application tool execution and continuation', 'limitations': [ 'Does not establish model availability, account access, latency, cost or output quality.', 'No real provider export, import, model migration, tool execution or data recovery was tested.', 'No streaming, refusals, errors, multimodal content or SDK helpers were exercised.', 'Illustrative parser accepts the fixture shapes only; do not treat it as a complete client.' ], 'cases': results, 'documentation': 'https://developers.openai.com/api/docs/guides/migrate-to-responses' }, indent=2))