Since Odoo 12 runs on Python 3.5 and the official openai package requires Python 3.6+, here are your best options:
Option 1: Use OpenAI API Directly (Recommended)
Instead of using the official Python package, make direct HTTP requests to the OpenAI API:
- Install requests package (works with Python 3.5):
bashpip install requests
- Create a custom module with this basic implementation:
pythonimport requests
import json
from odoo import models, fields, api
class OpenAIIntegration(models.Model):
_name = 'openai.integration'
def call_openai(self, prompt):
api_key = "your-api-key" # Store this securely in config parameters
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "text-davinci-003", # or newer model
"prompt": prompt,
"temperature": 0.7,
"max_tokens": 256
}
try:
response = requests.post(
"https://api.openai.com/v1/completions",
headers=headers,
data=json.dumps(data)
return response.json().get('choices')[0].get('text')
except Exception as e:
return f"Error: {str(e)}"
Option 2: Use Older OpenAI Package Version
Try version 0.10.8 (last version supporting Python 3.5):
bash
pip install openai==0.10.8
Option 3: Docker Workaround (Advanced)
Run a separate Python 3.6+ microservice that handles OpenAI calls and communicates with Odoo via:
- REST API
- XML-RPC
- Message queue (RabbitMQ)
Implementation Example
To generate report summaries from notes:
python
@api.model
def generate_summary(self, note_id):
note = self.env['note.note'].browse(note_id)
prompt = f"Summarize this business note in 3 bullet points:\n\n{note.name}"
summary = self.call_openai(prompt)
note.write({'summary': summary})
Important Notes
- Security: Never hardcode API keys - use ir.config_parameter
- Error Handling: Add proper timeouts and retries
- Rate Limits: Implement throttling (OpenAI has strict limits)
- Cost Control: Monitor token usage to avoid surprise bills
Alternative Approach
Consider upgrading to Odoo 13+ (Python 3.6+) if possible, as this will give you:
- Better Python version support
- Official OpenAI package compatibility
Security updates
🚀 Did This Solve Your
Problem?
If this answer helped you save time, money, or
frustration, consider:
✅ Upvoting (👍)
to help others find it faster
✅ Marking
as "Best Answer" if it resolved your issue
Your feedback keeps the Odoo community strong! 💪
(Need further customization? Drop a comment—I’m happy to
refine the solution!)
👋 Hi everyone,
Huge thanks to @Piyush H and @Desk Enterprise for your clear and helpful answers!
✅ I followed your advice and used direct HTTP requests with requests, which works perfectly with Odoo 12 (Python 3.5).
This approach is clean, effective, and fully compatible—no need to upgrade Python or use the official OpenAI library.
🔥 For anyone wondering: using the API directly without the openai package is a great workaround for legacy environments.
Thanks again and best of luck to all of you!
Amazigh