Assistants API URL Error - Invalid URL

Im getting an invalid url error when trying to run an assistant and calling the API, here’s my code:

from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import requests

Initialize the Flask application

app = Flask(name)
CORS(app, resources={r"/api/": {“origins”: ""}}) # Enable CORS for /api/ routes

Replace with your actual OpenAI API key

OPENAI_API_KEY = ‘your-openai-api-key’

@app.route(‘/’)
def index():
# Serve the index.html file
return send_from_directory(app.root_path, ‘index.html’)

@app.route(‘/api/message’, methods=[‘POST’])
def send_message_to_assistant():
# Get user input from the request
user_input = request.json.get(‘input’)

try:
    # Send the input to the OpenAI assistant
    response = requests.post(
        'https://api.openai.com/v1/assistants/asst_Q**************************o/messages',
        headers={
            'Authorization': f'Bearer sk-s*******************************************6',
            'Content-Type': 'application/json',
            'OpenAI-Beta': 'assistants=v1'
        },
        json={'input': user_input}
    )
    response.raise_for_status()  # Raises an HTTPError for bad responses
    return jsonify(response.json())
except requests.exceptions.RequestException as e:
    # Log any errors that occur during the API request
    return jsonify({'error': str(e)}), 500

if name == ‘main’:
# Run the Flask app
app.run(port=8000)

Ive been racking my brain for hours…
Checked in Postman also, same error:

{
“error”: {
“message”: “Invalid URL (POST /v1/assistants/asst_******************************/messages)”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null
}
}

Any help would be appreciated! Thanks in advance!

I think that you need to create the Message against a Thread rather than an Assistant:

POST https://api.openai.com/v1/threads/{thread_id}/messages

https://platform.openai.com/docs/api-reference/messages/createMessage

Thank you, so we cannot call the assistant with the settings it already has?

Mike

I can’t see anything in the API docs that lets you create a message outside the context of a Thread!

What are you trying to achieve?