Skip to main content

metorial-openai

OpenAI provider integration for Metorial.

Installation

pip install metorial openai

Quick Start

import asyncio
from metorial import Metorial, MetorialOpenAI
from openai import AsyncOpenAI

metorial = Metorial(api_key="your-metorial-api-key")
openai = AsyncOpenAI(api_key="your-openai-api-key")

async def main():
    async def session_handler(session):
        messages = [{"role": "user", "content": "What's the latest news?"}]

        for _ in range(10):
            response = await openai.chat.completions.create(
                model="gpt-4o",
                messages=messages,
                tools=session["tools"]
            )

            choice = response.choices[0]
            tool_calls = choice.message.tool_calls

            if not tool_calls:
                print(choice.message.content)
                break

            tool_responses = await session["callTools"](tool_calls)
            messages.append({"role": "assistant", "tool_calls": tool_calls})
            messages.extend(tool_responses)

        await session["closeSession"]()

    await metorial.with_provider_session(
        MetorialOpenAI.chat_completions,
        {"serverDeployments": [{"serverDeploymentId": "your-server-deployment-id"}]},
        session_handler
    )

asyncio.run(main())

Streaming

import asyncio
from metorial import Metorial, MetorialOpenAI
from openai import AsyncOpenAI

metorial = Metorial(api_key="your-metorial-api-key")
openai = AsyncOpenAI(api_key="your-openai-api-key")

async def main():
    async def session_handler(session):
        messages = [{"role": "user", "content": "What's the latest news?"}]

        stream = await openai.chat.completions.create(
            model="gpt-4o",
            messages=messages,
            tools=session["tools"],
            stream=True
        )

        async for chunk in stream:
            if chunk.choices[0].delta.content:
                print(chunk.choices[0].delta.content, end="", flush=True)

        await session["closeSession"]()

    await metorial.with_provider_session(
        MetorialOpenAI.chat_completions,
        {
            "serverDeployments": [{"serverDeploymentId": "your-server-deployment-id"}],
            "streaming": True,  # Required for streaming with tool calls
        },
        session_handler
    )

asyncio.run(main())

Supported Models

All OpenAI models that support function calling:

  • gpt-4o: Latest GPT-4o
  • gpt-4.1: GPT-4.1
  • o1: OpenAI o1
  • o3: OpenAI o3
  • gpt-4-turbo: GPT-4 Turbo
  • gpt-3.5-turbo: GPT-3.5 Turbo

Session Object

async def session_handler(session):
    tools = session["tools"]           # Tool definitions in OpenAI format
    call_tools = session["callTools"]  # Execute tools and get responses
    close_session = session["closeSession"]  # Close the session when done

Error Handling

from metorial import MetorialAPIError

try:
    await metorial.with_provider_session(...)
except MetorialAPIError as e:
    print(f"API Error: {e.message} (Status: {e.status})")
except Exception as e:
    print(f"Unexpected error: {e}")

License

MIT License - see LICENSE file for details.

Metadata

Release files for metorial-openai 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metorial-openai 1.0.6
File Size Uploaded
metorial_openai-1.0.6.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for metorial-openai 1.0.6
File Interpreter ABI Platform
metorial_openai-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / metorial_openai-1.0.6.tar.gz

Download URL metorial_openai-1.0.6.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c65c4f8a19db9b8054c52904c542889d634d49b06460052495a3fc8b11bcec49
BLAKE2b-256 checksum
How to use checksums
e3ecdf8201fe7db495e971067974cd5814c496d1f1c271adc4a0b8f510301dbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 23, 2025.

Transparency log

Release files / metorial_openai-1.0.6-py3-none-any.whl

Download URL metorial_openai-1.0.6-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f9d43bf4d8a3dccbf150003b70cd96b799f4fee1d079c194d4d929dde74f9f4
BLAKE2b-256 checksum
How to use checksums
6c1149ba921fd4aa71d33e8d7319e67e1e45a7e3302b74baaa9b518d5e2c6b93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 23, 2025.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page