examples.test_openai_compatibility¶
OpenAI API Compatibility Test Script for vllm-mlx.
View the complete module source at #L1-L739.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
examples.test_openai_compatibility
¶
OpenAI API Compatibility Test Script for vllm-mlx.
This script tests the OpenAI API compatibility of the vllm-mlx server. It tests both the direct HTTP API and the official OpenAI Python client.
Usage
First start the server:¶
vllm-mlx serve --served-model-name default mlx-community/Qwen3-VL-4B-Instruct-3bit --port 8000
Then run this script:¶
python examples/test_openai_compatibility.py
With a different server URL:¶
python examples/test_openai_compatibility.py --server-url http://localhost:9000
Test only specific endpoints:¶
python examples/test_openai_compatibility.py --test-image --test-video
examples.test_openai_compatibility.print_header
¶
examples.test_openai_compatibility.print_test
¶
Print test result.
examples.test_openai_compatibility.print_warning
¶
examples.test_openai_compatibility.create_test_image
¶
Create a simple test image and return (path, bytes).
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_health_endpoint
¶
Test the /health endpoint.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_models_endpoint
¶
Test the /v1/models endpoint.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_chat_completions_http
¶
Test /v1/chat/completions with direct HTTP.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_chat_completions_openai
¶
Test /v1/chat/completions with OpenAI Python client.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_completions_endpoint
¶
Test /v1/completions endpoint (legacy).
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_image_chat_http
¶
Test multimodal image chat with direct HTTP.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_image_chat_openai
¶
Test multimodal image chat with OpenAI client.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_image_url_http
¶
Test image from URL.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_streaming_chat
¶
Test streaming chat completions.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.create_test_video
¶
Create a simple test video with colored frames.
Returns (path, bytes) of a minimal MP4 video.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_video_chat_http
¶
Test multimodal video chat with direct HTTP.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_video_chat_openai
¶
Test multimodal video chat with OpenAI client.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.test_video_url_http
¶
Test video from URL.
Source code in examples/test_openai_compatibility.py
examples.test_openai_compatibility.run_all_tests
¶
Run all compatibility tests.
Source code in examples/test_openai_compatibility.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
examples.test_openai_compatibility.main
¶
Source code in examples/test_openai_compatibility.py
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
examples.test_openai_compatibility.print_header · function
Print a section header.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated
Exceptions and behavior
Function print_header calls print.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.print_test · function
examples.test_openai_compatibility.print_test(name: str, passed: bool, message: str = '') -> not annotated
Print test result.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name |
str |
yes |
none |
Required positional or keyword input. |
passed |
bool |
yes |
none |
Required positional or keyword input. |
message |
str |
no |
'' |
Optional positional or keyword input; defaults to ''. |
Returns
- Type:
not annotated
Exceptions and behavior
Function print_test calls print.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.print_warning · function
Print a warning message.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated
Exceptions and behavior
Function print_warning calls print.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.create_test_image · function
Create a simple test image and return (path, bytes).
Parameters
This callable has no explicit inputs.
Returns
- Type:
tuple[str, bytes] - Direct return expressions:
(temp_file.name, img_bytes);(temp_file.name, minimal_png)
Exceptions and behavior
Function create_test_image calls Image.new, io.BytesIO, img.save, buffer.getvalue; has 2 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_health_endpoint · function
Test the /health endpoint.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
response.status_code == 200;False
Exceptions and behavior
Function test_health_endpoint calls requests.get, print_warning; has 2 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_models_endpoint · function
Test the /v1/models endpoint.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
False;'data' in data and isinstance(data['data'], list)
Exceptions and behavior
Function test_models_endpoint calls requests.get, response.json, isinstance, print_warning; has 2 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_chat_completions_http · function
Test /v1/chat/completions with direct HTTP.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, f'Status code: {response.status_code}');(False, "Missing 'choices' in response");(False, 'Empty choices array');(False, "Missing 'message' in choice");(False, "Missing 'content' in message");(True, f'Response: {content[:50]}...');(False, str(e))
Exceptions and behavior
Function test_chat_completions_http calls requests.post, response.json, len, str; has 7 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_chat_completions_openai · function
examples.test_openai_compatibility.test_chat_completions_openai(server_url: str) -> tuple[bool, str]
Test /v1/chat/completions with OpenAI Python client.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, 'OpenAI package not installed. Run: pip install openai');(True, f'Response: {content[:50]}...');(False, str(e))
Exceptions and behavior
Function test_chat_completions_openai calls OpenAI, client.chat.completions.create, str; has 3 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_completions_endpoint · function
Test /v1/completions endpoint (legacy).
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, f'Status code: {response.status_code}');(False, "Missing 'choices' in response");(False, 'Empty choices array');(True, f'Response: {text[:50]}...');(False, str(e))
Exceptions and behavior
Function test_completions_endpoint calls requests.post, response.json, len, data['choices'][0].get; has 5 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_image_chat_http · function
Test multimodal image chat with direct HTTP.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, f'Status code: {response.status_code}, Body: {response.text[:100]}');(True, f'Response: {content[:50]}...');(False, str(e))
Exceptions and behavior
Function test_image_chat_http calls create_test_image, base64.b64encode(image_bytes).decode, base64.b64encode, requests.post; has 3 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_image_chat_openai · function
Test multimodal image chat with OpenAI client.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, 'OpenAI package not installed');(True, f'Response: {content[:50]}...');(False, str(e))
Exceptions and behavior
Function test_image_chat_openai calls create_test_image, base64.b64encode(image_bytes).decode, base64.b64encode, OpenAI; has 3 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_image_url_http · function
Test image from URL.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, f'Status code: {response.status_code}');(True, f'Response: {content[:80]}...');(False, str(e))
Exceptions and behavior
Function test_image_url_http calls requests.post, response.json, str; has 3 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_streaming_chat · function
Test streaming chat completions.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, f'Status code: {response.status_code}');(False, 'No streaming chunks received');(True, f'Received {len(chunks)} streaming chunks');(False, str(e))
Exceptions and behavior
Function test_streaming_chat calls requests.post, response.iter_lines, line.decode, line.startswith; has 4 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.create_test_video · function
Create a simple test video with colored frames.
Parameters
This callable has no explicit inputs.
Returns
- Type:
tuple[str, bytes] - Direct return expressions:
(temp_path, video_bytes);(None, None)
Exceptions and behavior
Function create_test_video calls tempfile.NamedTemporaryFile, temp_file.close, cv2.VideoWriter_fourcc, cv2.VideoWriter; has 2 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_video_chat_http · function
Test multimodal video chat with direct HTTP.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, 'Could not create test video (OpenCV required)');(False, f'Status code: {response.status_code}, Body: {response.text[:100]}');(True, f'Response: {content[:80]}...');(False, str(e))
Exceptions and behavior
Function test_video_chat_http calls create_test_video, base64.b64encode(video_bytes).decode, base64.b64encode, requests.post; has 4 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_video_chat_openai · function
Test multimodal video chat with OpenAI client.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, 'OpenAI package not installed');(False, 'Could not create test video (OpenCV required)');(True, f'Response: {content[:80]}...');(False, str(e))
Exceptions and behavior
Function test_video_chat_openai calls create_test_video, base64.b64encode(video_bytes).decode, base64.b64encode, OpenAI; has 4 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.test_video_url_http · function
Test video from URL.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[bool, str] - Direct return expressions:
(False, f'Status code: {response.status_code}');(True, f'Response: {content[:80]}...');(False, str(e))
Exceptions and behavior
Function test_video_url_http calls requests.post, response.json, str; has 3 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.run_all_tests · function
examples.test_openai_compatibility.run_all_tests(server_url: str, test_image: bool = True, test_video: bool = True) -> not annotated
Run all compatibility tests.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server_url |
str |
yes |
none |
Required positional or keyword input. |
test_image |
bool |
no |
True |
Optional positional or keyword input; defaults to True. |
test_video |
bool |
no |
True |
Optional positional or keyword input; defaults to True. |
Returns
- Type:
not annotated - Direct return expressions:
0;1
Exceptions and behavior
Function run_all_tests calls print_header, print, test_health_endpoint, print_test; has 2 explicit return paths.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.run_all_tests.record · nested function
Nested Function run_all_tests.record contains no state mutation, call, raise, return, await, or yield.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
passed |
bool |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated
Exceptions and behavior
Nested Function run_all_tests.record contains no state mutation, call, raise, return, await, or yield.
No direct raise statement appears in this definition.
examples.test_openai_compatibility.main · function
Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print; returns run_all_tests(server_url=args.server_url, test_image=not args.no_image, test_video=not args.no_video).
Parameters
This callable has no explicit inputs.
Returns
- Type:
not annotated - Direct return expressions:
run_all_tests(server_url=args.server_url, test_image=not args.no_image, test_video=not args.no_video)
Exceptions and behavior
Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print; returns run_all_tests(server_url=args.server_url, test_image=not args.no_image, test_video=not args.no_video).
No direct raise statement appears in this definition.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
print_header |
function | print_header(text: str) -> not annotated |
Print a section header. | #L37-L41 |
print_test |
function | print_test(name: str, passed: bool, message: str = '') -> not annotated |
Print test result. | #L44-L49 |
print_warning |
function | print_warning(text: str) -> not annotated |
Print a warning message. | #L52-L54 |
create_test_image |
function | create_test_image() -> tuple[str, bytes] |
Create a simple test image and return (path, bytes). | #L57-L97 |
test_health_endpoint |
function | test_health_endpoint(server_url: str) -> bool |
Test the /health endpoint. | #L100-L109 |
test_models_endpoint |
function | test_models_endpoint(server_url: str) -> bool |
Test the /v1/models endpoint. | #L112-L126 |
test_chat_completions_http |
function | test_chat_completions_http(server_url: str) -> tuple[bool, str] |
Test /v1/chat/completions with direct HTTP. | #L129-L170 |
test_chat_completions_openai |
function | test_chat_completions_openai(server_url: str) -> tuple[bool, str] |
Test /v1/chat/completions with OpenAI Python client. | #L173-L199 |
test_completions_endpoint |
function | test_completions_endpoint(server_url: str) -> tuple[bool, str] |
Test /v1/completions endpoint (legacy). | #L202-L233 |
test_image_chat_http |
function | test_image_chat_http(server_url: str) -> tuple[bool, str] |
Test multimodal image chat with direct HTTP. | #L236-L278 |
test_image_chat_openai |
function | test_image_chat_openai(server_url: str) -> tuple[bool, str] |
Test multimodal image chat with OpenAI client. | #L281-L322 |
test_image_url_http |
function | test_image_url_http(server_url: str) -> tuple[bool, str] |
Test image from URL. | #L325-L363 |
test_streaming_chat |
function | test_streaming_chat(server_url: str) -> tuple[bool, str] |
Test streaming chat completions. | #L366-L405 |
create_test_video |
function | create_test_video() -> tuple[str, bytes] |
Create a simple test video with colored frames. | #L408-L449 |
test_video_chat_http |
function | test_video_chat_http(server_url: str) -> tuple[bool, str] |
Test multimodal video chat with direct HTTP. | #L452-L498 |
test_video_chat_openai |
function | test_video_chat_openai(server_url: str) -> tuple[bool, str] |
Test multimodal video chat with OpenAI client. | #L501-L546 |
test_video_url_http |
function | test_video_url_http(server_url: str) -> tuple[bool, str] |
Test video from URL. | #L549-L587 |
run_all_tests |
function | run_all_tests(server_url: str, test_image: bool = True, test_video: bool = True) -> not annotated |
Run all compatibility tests. | #L590-L684 |
run_all_tests.record |
nested function | run_all_tests.record(passed: bool) -> not annotated |
Nested Function run_all_tests.record contains no state mutation, call, raise, return, await, or yield. |
#L594-L598 |
main |
function | main() -> not annotated |
Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print; returns run_all_tests(server_url=args.server_url, test_image=not args.no_image, test_video=not args.no_video). |
#L687-L735 |