I have a python application, that uses uvicorn for local run and docker when built. I got a new error that I haven't got in the previous version, the error only happens in docker but not when using uvicorn directly. I haven't changed any settings in the dockerfile from the previous version that worked, to the new one which only works running locally uvicorn.
What could be the cause of this error?
Traceback (most recent call last):
File "/usr/src/app/main.py", line 58, in run_supervisor
response, usage_total_data = await assistant_run(content)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/app/controllers/assistant_controller.py", line 10, in assistant_run
graph = create_graph_assistant(config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/app/services/graph_services.py", line 28, in create_graph_assistant
supervisor_node = Supervisor(config, 'supervisor')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/app/nodes/supervisor_node.py", line 12, in init
super().init(config, name)
File "/usr/src/app/nodes/base_node.py", line 16, in init
self.llm = ChatOpenAI(
^^^^^^^^^^^
File "/usr/src/venv/lib/python3.12/site-packages/langchain_core/load/serializable.py", line 125, in init
super().init(*args, **kwargs)
File "/usr/src/venv/lib/python3.12/site-packages/pydantic/main.py", line 214, in init
validated_self = self.pydantic_validator.validate_python(data, self_instance=self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/venv/lib/python3.12/site-packages/langchain_openai/chat_models/base.py", line 534, in validate_environment
self.root_client = openai.OpenAI(**client_params, **sync_specific) # type: ignore[arg-type]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/venv/lib/python3.12/site-packages/openai/_client.py", line 123, in init
super().init(
File "/usr/src/venv/lib/python3.12/site-packages/openai/_base_client.py", line 849, in init
self._client = http_client or SyncHttpxClientWrapper(
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/venv/lib/python3.12/site-packages/openai/_base_client.py", line 747, in init
super().init(**kwargs)
TypeError: Client.init() got an unexpected keyword argument 'proxies'
2024-12-03 12:29:01,104 - ERROR - Error al ejecutar el asistente: Client.init() got an unexpected keyword argument 'proxies'
INFO: 172.17.0.1:56284 - "POST /run-supervisor/ HTTP/1.1" 200 OK
Assistant
User
I haven't changed my libraries nor the requirements.txt, so the docker should have the same versions. I haven't added any code with proxies nor it is a keyword I have in my entire project when searching for it.
Assistant
This error most likely stems from a version mismatch between the OpenAI Python library (or its dependencies) in your Docker container and your local development environment. Specifically, the issue is that the Client.__init__() method is being passed a proxies keyword argument, which is not recognized in the currently installed version of the OpenAI library in your Docker container.
Here are the likely causes and steps to troubleshoot and resolve the issue:
Problem Analysis:
Locally, your application works because the OpenAI library installed on your development machine may be older (or newer) than the version you are using in Docker. The library in the Docker container might have modified or removed the proxies keyword argument signature.
This behavior typically happens when there are dependency mismatches between environments. If your Docker container is using an outdated or newer version of the library, the constructor for openai.Client.__init__() won't accept the proxies argument anymore (or expects it in a different way).
Steps to Resolve:
1. Check the Installed OpenAI Python Library Version
In your local environment, check the version of the OpenAI library:
bash
pip show openai
Look for the Version information (e.g., 0.x.x).
In your Docker container:
Run the same command inside your Docker container:
bash
docker exec -it <your_container_name> bash
pip show openai
Again, note the version. If there's a mismatch between your local environment and Docker, you need to align the versions.
Alternatively, ensure that your Docker build uses consistent versions between environments. For example, specify an exact version for OpenAI in your requirements.txt or pyproject.toml:
code
openai==X.X.X
2. Fix Dependency Mismatches
Add stricter versioning control for your Python dependencies. There are two approaches:
Using requirements.txt
If your project uses a requirements.txt file, explicitly pin the version of the OpenAI library:
code
openai==<version_you_use_locally>
Then, rebuild your Docker image:
bash
docker build --no-cache -t your-image-name .
Using Dependency Synchronization
Use tools to synchronize your local environment and Docker:
Freeze your current Python dependencies:
bash
pip freeze > requirements.txt
Rebuild Docker with the frozen dependencies:
bash
docker build --no-cache -t your-image-name .
3. Upgrade or Downgrade the OpenAI Library
What happened is most likely due to a breaking change or API inconsistency:
If you want the latest version of the OpenAI library:
bash
pip install --upgrade openai
Or, downgrade the OpenAI library to the version you are using locally:
bash
pip install openai==<your_local_version>
4. Review the OpenAI and LangChain Versions
Since you rely on LangChain (langchain_core.load.serializable) in conjunction with OpenAI, make sure the versions of both libraries are compatible:
Check the version of LangChain locally and in Docker:
bash
pip show langchain
Look specifically at known compatibility between LangChain and OpenAI libraries. Reference the LangChain GitHub repository or documentation for their compatible dependency table.
5. Validate Environment-Sensitive Settings
The Docker environment may pass sensitive data such as proxies or configuration differently than the local environment. Check where proxies are being introduced:
Search for the proxies keyword in your codebase, configuration files, or environment variables.
Ensure consistent environment variable definitions and secrets between local and Docker:
dockerfile
ENV OPENAI_API_KEY="your-api-key"
6. Debug the Build and Runtime Environment
Add logging for the versions and settings being used. Modify your main.py or the entrypoint script to log installed library versions and relevant configurations:
If you cannot resolve the issue immediately, you can modify your code to remove or handle the proxies argument:
Locate where proxies is being passed to openai in your configuration or codebase.
Remove or conditionally exclude the argument:
python
# Example assuming `client_params` may contain 'proxies':
client_params.pop('proxies', None)
openai_client = openai.OpenAI(**client_params, **sync_specific)
This eliminates the immediate error, but you should still aim to resolve the root cause by fixing the dependency misalignment.
Summary
The error originates from an inconsistent or incompatible OpenAI library version used in Docker. Pin your dependencies to known working versions and ensure they are consistent between your local and Docker environments. Troubleshoot by logging and investigating where proxies is introduced in your codebase or configuration.
Thank you for providing additional details! If neither your dependencies nor the codebase has changed, and you haven't introduced any proxies logic, the issue still points to some difference between your local environment and your Docker environment. However, this rules out direct changes to your code or dependencies and suggests something else influencing the Docker runtime. Let's explore deeper causes and solutions.
Potential causes of this issue
Environment Variables or Docker Configuration:
When running in Docker, the proxies configuration might be injected or set automatically. This could be caused by:
Docker's network settings or proxy settings within the environment.
An open-source library emitting a proxies argument (indirectly), e.g., your LangChain or OpenAI dependency loading a proxy configuration from the environment.
The base Docker image or environment configuration.
Likely suspect: Unwanted proxy settings might be introduced implicitly.
Docker-Only Networking or Proxy Settings:
Your local machine might have direct internet access, while Docker uses specific networking-related configurations (proxy settings might be automatically injected or inherited).
If Docker uses a company or local environment proxy, the OpenAI library tries to use the proxies key with malformed arguments, leading to this error.
Dependency-Specific Changes:
While you haven't explicitly updated libraries, external dependencies could have internally updated or refactored their usage due to intermediary changes (e.g., LangChain, OpenAI, or even httpx internals). Docker builds a fresh environment every time, so it may automatically pull newer sub-versions or patch releases of nested dependencies.
Steps to Diagnose and Debug
Since the issue is appearing only in Docker, here's how to identify the cause:
Step 1: Inspect Environment Variables
Docker may pass proxy configurations via environment variables (HTTP_PROXY, HTTPS_PROXY, or ALL_PROXY) either explicitly or implicitly.
Run this command inside your Docker container to see if proxy-related environment variables exist:
If any proxy-related environment variables (HTTP_PROXY, HTTPS_PROXY, etc.) are set in the Docker environment, the OpenAI or httpx library may be auto-configuring itself to use them.
Action: Either unset the variables or ensure they are properly configured using Docker's network settings.
Step 2: Log Library Versions in Docker
Since library version mismatches or transitive dependency updates might be at play, print the versions of relevant libraries in your application during container startup.
Add the following snippet early in your main.py or entry script:
Check whether the library versions inside Docker differ from those locally. If the versions mismatch or transitive dependencies differ (e.g., httpx), you may need to enforce stricter dependency control by explicitly pinning them.
Step 3: Examine Dependency Behavior in Docker
Check whether proxies is possibly introduced implicitly. While proxies isn't part of your codebase, the httpx library (used by openai or a transitive dependency) might be the source.
Run this diagnostic test locally and in Docker to inspect keyword arguments passed to the constructor of openai.Client:
If proxies shows up unexpectedly for the Docker run, it could indicate implicit behavior added in the Docker environment.
Step 4: Manually Debug proxies Injection
You can debug how proxies is being propagated in the code. Since this comes from the stack trace originating in OpenAI.__init__(), inject debug prints there to inspect the unexpected proxies argument.
Modify temporary sections of the OpenAI library (or dynamically patch it) to dump kwargs received by the constructor.
In your main.py:
python
import openai
class DebugOpenAI(openai.OpenAI):
def __init__(self, *args, **kwargs):
print("OpenAI client args:", args)
print("OpenAI client kwargs:", kwargs)
super().__init__(*args, **kwargs)
# Later in the code:
openai_client = DebugOpenAI()
Rebuild the Docker image, rerun it, and inspect the arguments to openai.Client.
Step 5: Force Dependency Consistency
Even if you haven't changed your requirements.txt, indirect dependencies may differ when pip installs your dependencies for Docker. Use pip freeze to pin exactly what is working locally and enforce the same within Docker.
Lock Current Dependency Versions:
On your local machine:
bash
pip freeze > requirements.txt
Rebuild and Test Docker with the Frozen Dependencies:
If proxies are being set automatically at runtime, explicitly reset or remove proxy settings in code:
python
import os
# Disable proxies globally
os.environ.pop("HTTP_PROXY", None)
os.environ.pop("HTTPS_PROXY", None)
os.environ.pop("ALL_PROXY", None)
# Remove proxies from OpenAI params
client_params.pop("proxies", None)
Root Cause Suspicions
Proxy-related environment variables like HTTP_PROXY or HTTPS_PROXY are being injected in Docker by the base image, host machine, or environment configuration.
A dependency implicitly sets the proxies argument (likely within the httpx library used by openai).
Transitive dependency versions differ between your Docker and local environments (e.g., httpx, langchain, or openai).
Proposed Resolution
Inspect for the presence of proxy environment variables in Docker. Remove or update them if necessary.
Align dependency versions between local and Docker environments by freezing pip freeze output.
Log or dynamically debug where proxies is introduced via diagnostics like DebugOpenAI or updating the libraries.
If proxy logic isn't required, eliminate or exclude proxies in the code by .pop('proxies', None).
By fixing the environment or standardizing dependency resolution, this issue can be resolved cleanly.