Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

from openai import OpenAI

Example of Calling OmniRoute Using the Python Library openai

client = OpenAI(
    base_url="http://localhost:20128/v1",
    api_key="YOUR_OMNIROUTE_API_KEY" # Generated in your local OmniRoute dashboard
)

response = client.chat.completions.create(
    model="auto/best-coding", # OmniRoute handles the routing behind the scenes
    messages=[
        {
            "role": "user",
            "content": "Write a Python function to reverse a string. Output the code ONLY."
        }
    ]
)
response
ChatCompletion(id='router-27eac306f9725fd458c970d837a083b6', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='def reverse_string(s):\n return s[::-1]', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=None, reasoning_content='We need to output Python code for reversing a string. The instruction says "Write a Python function to reverse a string. Output the code ONLY." So we just give the function definition. Probably simple: def reverse_string(s): return s[::-1]. Or using reversed. We\'ll output code only, no explanation. Ensure it\'s a function. Let\'s do:\n\ndef reverse_string(s):\n return s[::-1]\n\nThat\'s it.'))], created=1786923527, model='deepseek-v4-flash-free', object='chat.completion', moderation=None, service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=101, prompt_tokens=97, total_tokens=198, completion_tokens_details=None, prompt_tokens_details=PromptTokensDetails(audio_tokens=None, cache_write_tokens=None, cached_tokens=None)))
dir(response)
['__abstractmethods__', '__annotate_func__', '__annotations__', '__annotations_cache__', '__class__', '__class_getitem__', '__class_vars__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__fields__', '__fields_set__', '__firstlineno__', '__format__', '__ge__', '__get_pydantic_core_schema__', '__get_pydantic_json_schema__', '__getattr__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__pretty__', '__private_attributes__', '__pydantic_complete__', '__pydantic_computed_fields__', '__pydantic_core_schema__', '__pydantic_custom_init__', '__pydantic_decorators__', '__pydantic_extra__', '__pydantic_fields__', '__pydantic_fields_set__', '__pydantic_generic_metadata__', '__pydantic_init_subclass__', '__pydantic_on_complete__', '__pydantic_parent_namespace__', '__pydantic_post_init__', '__pydantic_private__', '__pydantic_root_model__', '__pydantic_serializer__', '__pydantic_setattr_handlers__', '__pydantic_validator__', '__reduce__', '__reduce_ex__', '__replace__', '__repr__', '__repr_args__', '__repr_name__', '__repr_recursion__', '__repr_str__', '__rich_repr__', '__setattr__', '__setstate__', '__signature__', '__sizeof__', '__slots__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_calculate_keys', '_copy_and_set_values', '_get_value', '_iter', '_request_id', '_setattr_handler', 'choices', 'construct', 'copy', 'created', 'dict', 'from_orm', 'id', 'json', 'model', 'model_computed_fields', 'model_config', 'model_construct', 'model_copy', 'model_dump', 'model_dump_json', 'model_extra', 'model_fields', 'model_fields_set', 'model_json_schema', 'model_parametrized_name', 'model_post_init', 'model_rebuild', 'model_validate', 'model_validate_json', 'model_validate_strings', 'moderation', 'object', 'parse_file', 'parse_obj', 'parse_raw', 'schema', 'schema_json', 'service_tier', 'system_fingerprint', 'to_dict', 'to_json', 'update_forward_refs', 'usage', 'validate']
response.choices
[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='def reverse_string(s):\n return s[::-1]', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=None, reasoning_content='We need to output Python code for reversing a string. The instruction says "Write a Python function to reverse a string. Output the code ONLY." So we just give the function definition. Probably simple: def reverse_string(s): return s[::-1]. Or using reversed. We\'ll output code only, no explanation. Ensure it\'s a function. Let\'s do:\n\ndef reverse_string(s):\n return s[::-1]\n\nThat\'s it.'))]
response.choices[0]
Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='def reverse_string(s):\n return s[::-1]', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=None, reasoning_content='We need to output Python code for reversing a string. The instruction says "Write a Python function to reverse a string. Output the code ONLY." So we just give the function definition. Probably simple: def reverse_string(s): return s[::-1]. Or using reversed. We\'ll output code only, no explanation. Ensure it\'s a function. Let\'s do:\n\ndef reverse_string(s):\n return s[::-1]\n\nThat\'s it.'))
print(response.choices[0].message.content)
def reverse_string(s):
    return s[::-1]
help(response.construct)
Help on method construct in module openai._models:

construct(_fields_set: set[str] | None = None, **values: object) -> ModelT class method of openai.types.chat.chat_completion.ChatCompletion
    # Override the 'construct' method in a way that supports recursive parsing without validation.
    # Based on https://github.com/samuelcolvin/pydantic/issues/1168#issuecomment-817742836.

The Model That the Requested Routed to

response.model
'deepseek-v4-flash-free'