Skip to main content

Securely Dumping and Loading an OpenAI Client with Pickle

Project description

pickle-openai

PyPI version License: MIT

Securely serialize and deserialize OpenAI client instances.

This library allows you to safely store your OpenAI client configuration (including API keys) in an encrypted format. It uses pickle for serialization and cryptography for strong encryption, requiring a password to protect your data.

Installation

pip install pickle-openai

Usage

Here's how to securely dump an OpenAI client to an encrypted string and load it back.

import os
from pickle_openai import PickledOpenAI
from openai import OpenAI

# 1. Initialize your OpenAI client
# Make sure OPENAI_API_KEY is set in your environment
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

# 2. Create a PickledOpenAI instance from your client
pickled_client = PickledOpenAI.from_openai(client)

# 3. Dump the client to a password-protected, encrypted string
password = "your-strong-password"
encrypted_string = pickled_client.pickle_dumps(password)

print(f"Encrypted client string: {encrypted_string}")

# You can now store this encrypted_string safely.

# 4. Load the client back from the encrypted string
loaded_pickled_client = PickledOpenAI.pickle_loads(encrypted_string, password)

# 5. Recreate the OpenAI client
recreated_client = loaded_pickled_client.to_openai_sync()

# The recreated client is ready to use
response = recreated_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello world!"}],
)
print(f"OpenAI API Response: {response.choices[0].message.content}")

This works for both openai.OpenAI and openai.AzureOpenAI clients (and their async versions).

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pickle_openai-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pickle_openai-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pickle_openai-0.1.0.tar.gz.

File metadata

  • Download URL: pickle_openai-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.11.13 Darwin/24.5.0

File hashes

Hashes for pickle_openai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c7ca1a45c4d84655264d8c85c3799c1a6e44a2b4a34e65a31bd1adbafff635e7
MD5 e9a4a585d107411e2d01bbbc2aaf5be0
BLAKE2b-256 ec8b12e683aa800865762d4f0fb54f11e4f8bab39eb6f95717a273d162721a53

See more details on using hashes here.

File details

Details for the file pickle_openai-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pickle_openai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.11.13 Darwin/24.5.0

File hashes

Hashes for pickle_openai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb0c4539474e5168ecf84ee073a0f44978c5dea1a5bdd93c397f0ffc3254dc77
MD5 574c5c0af2f5b15294ef9b1347140383
BLAKE2b-256 29eb979610654bf306929e0a78194b76db3634ea867027031037bce6f66d801f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page