Skip to main content

custom-openai

A robust, user-friendly wrapper for the OpenAI Python package, featuring simplified response formatting and explicit error handling.

Features

  • Clean and consistent responses: Always get predictable output from your completions.
  • Automatic error handling: Exceptions are caught and returned in a structured way.
  • Async and sync support: Use the API in both synchronous and asynchronous Python projects.

Installation

pip install custom-openai

Usage

Synchronous

from custom_openai import CustomOpenAIClient

client = CustomOpenAIClient(api_key="your-api-key")
response = client.chat_completions_create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=100
)
if response["success"]:
    print(response["content"])
else:
    print("Error:", response["error_type"], response["error_message"])

Asynchronous

import asyncio
from custom_openai import CustomAsyncOpenAIClient

async def main():
    client = CustomAsyncOpenAIClient(api_key="your-api-key")
    response = await client.chat_completions_create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}],
        max_tokens=100
    )
    if response["success"]:
        print(response["content"])
    else:
        print("Error:", response["error_type"], response["error_message"])

asyncio.run(main())

Response Format

All responses follow the same structure:

{
    "success": True, # or False if there was an error
    "text": "...", # model output (only if success)
    "model": "...", # model used
    "usage": {
        "prompt_tokens": ...,
        "completion_tokens": ...,
        "total_tokens": ...
    },
    "finish_reason": "...", # reason for completion
    # Error fields (only present if success is False):
    "error_type": "...",
    "error_message": "..."
}

License

MIT License

Metadata

Release files for fq-test-release 0.8.0

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

Source distribution (sdist)

Source distribution for fq-test-release 0.8.0
File Size Uploaded
fq_test_release-0.8.0.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fq-test-release 0.8.0
File Interpreter ABI Platform
fq_test_release-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / fq_test_release-0.8.0.tar.gz

Download URL fq_test_release-0.8.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
38437bdec0c8a3733248f80df91ba29a093e7ad566efe754eafa3cab4c491703
BLAKE2b-256 checksum
How to use checksums
7e4dc23d2688b95461a2d38a477b715674d8cbd25cc46626d54f1d179c506302
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / fq_test_release-0.8.0-py3-none-any.whl

Download URL fq_test_release-0.8.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60462940b29f399e2f29d19724977c029ed0e488c350f162b894ba8f52161a3c
BLAKE2b-256 checksum
How to use checksums
757d7daeb99de114eb44e2b864822939d0f5f998b24e42d36163ae703c58e78d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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