Oracle Cloud Function receiving 404 "resource not found" error when accessing Generative AI Agent endpoint

I’m attempting to invoke an Oracle Generative AI Agent through a cloud function but I keep facing difficulties. I wish to combine this with Oracle APEX to allow the agent to execute SQL queries on the database.

Each time I call the API, I encounter the following error response:

{
    "status": "error",
    "response": "Failed to reach AI service: {
        'service': 'generative_ai_agent_runtime',
        'http_status': 404,
        'error_code': '404',
        'request_id': '9371582AD25143568BEFF3A88F2679CD/17594A13EC84969BF85E5A79576352BC/83AF4847BD2CC5F7B9D17F5EBG5G5206',
        'error_message': 'Resource not available 3cf33fc6-ec55-5ge8-c280-ef834bb513bb',
        'api_operation': 'chat',
        'error_time': '2025-06-11T10:42:18.445123+00:00',
        'sdk_version': 'Oracle-PythonSDK/2.154.1',
        'api_url': 'POST https://agent-runtime.generativeai.eu-frankfurt-1.oci.oraclecloud.com/20240531/agentEndpoints/ocid1.genaiagentendpoint.oc1.eu-frankfurt-1.amaaaaaabumsjqaa4ezfogklva8nrjyxfulsahguzn8hdmco4fwqrjg3amor/actions/chat'
    }"
}

Here’s the code for my function:

import json
import oci
import uuid
from oci.auth import signers

def invoke_ai_service(user_input):
    try:
        # Set up resource principal authentication
        auth = signers.get_resource_principals_signer()
        
        ai_client = oci.generative_ai_agent_runtime.GenerativeAiAgentRuntimeClient(
            config={}, signer=auth
        )
        
        conversation_id = str(uuid.uuid4())
        
        response = ai_client.chat(
            agent_endpoint_id="ocid1.genaiagentendpoint.oc1.eu-frankfurt-1.amaaaaaabumsjqaa4ezfogklva8nrjyxfulsahguzn8hdmco4fwqrjg3amor",
            chat_details=oci.generative_ai_agent_runtime.models.ChatDetails(
                user_message=user_input,
                session_id=conversation_id
            )
        )
        
        return response.data
        
    except Exception as error:
        return f"Failed to reach AI service: {str(error)}"

I’ve verified that both the agent and endpoint are created in the right region and are active in the console. The IAM policies appear correct and the dynamic group correctly includes my function. Since I’m not experiencing auth errors, what could be triggering this resource not found error?

I encountered a similar issue recently and it turned out to be related to the agent endpoint state rather than authentication. Even though the console shows the endpoint as “Active”, there can be deployment delays or the endpoint might still be initializing behind the scenes. Try checking the exact timestamp when you created the endpoint versus when you started getting these errors. In my case, I had to wait about 15-20 minutes after the status showed active before the endpoint was actually reachable. Also worth double-checking that your function’s region configuration matches exactly with where the endpoint was deployed - I once had a subtle mismatch where my code was pointing to the wrong regional subdomain. You might want to test the endpoint directly using OCI CLI first to isolate whether this is a function-specific issue or an endpoint availability problem.