# Registered agent using gemini returns API key not valid

**URL:** <https://forum.opensearch.org/t/registered-agent-using-gemini-returns-api-key-not-valid/27957>\
**Category:** Machine Learning\
**Created:** [March 30, 2026, 7:29pm UTC](https://forum.opensearch.org/t/registered-agent-using-gemini-returns-api-key-not-valid/27957 "2026-03-30T19:29:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gstorme](https://avatars.discourse-cdn.com/v4/letter/g/91b2a8/32.png) [@gstorme](https://forum.opensearch.org/u/gstorme)\
**Post date:** [March 30, 2026, 7:29pm UTC](https://forum.opensearch.org/t/registered-agent-using-gemini-returns-api-key-not-valid/27957/1 "2026-03-30T19:29:07Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser): 3.5.0

**Describe the issue** :

Following the documentation on [Register agent - OpenSearch Documentation](https://docs.opensearch.org/latest/ml-commons-plugin/api/agent-apis/register-agent/#unified-agent-registration) I have registered an agent:

```auto
POST /_plugins/_ml/agents/_register
{
  "name": "opensearch agent",
  "type": "conversational",
  "description": "Test conversational agent",
  "model": {
    "model_id": "gemini-2.5-pro",
    "model_provider": "gemini/v1beta/generatecontent",
    "credentials": {
      "api_key": "xyz"
    },
    "parameters": {
      "system_prompt": "You are an expert data analyst with access to OpenSearch indices"
    }
  },
  "tools": [
    {
      "type": "ListIndexTool"
    },
    {
      "type": "SearchIndexTool"
    }
  ],
  "memory": {
    "type": "conversation_index"
  }
}

```

But when testing it:

```auto
POST /_plugins/_ml/agents/TmMRQJ0Ba60ZVyos9x8l/_execute
{
  "input": "What tools do you have access to?"
}

```

It returns the following error:

```auto
{
  "status": 400,
  "error": {
    "type": "OpenSearchStatusException",
    "reason": "Invalid Request",
    "details": "Error from remote service: {\n \"error\": {\n \"code\": 400,\n \"message\": \"API key not valid. Please pass a valid API key.\",\n \"status\": \"INVALID_ARGUMENT\",\n \"details\": [\n {\n \"@type\": \"type.googleapis.com/google.rpc.ErrorInfo\",\n \"reason\": \"API_KEY_INVALID\",\n \"domain\": \"googleapis.com\",\n \"metadata\": {\n \"service\": \"generativelanguage.googleapis.com\"\n }\n },\n {\n \"@type\": \"type.googleapis.com/google.rpc.LocalizedMessage\",\n \"locale\": \"en-US\",\n \"message\": \"API key not valid. Please pass a valid API key.\"\n }\n]\n }\n}\n"
  }
}

```

The API key I’m using is correct, it’s working when used in another tool.

---

<div class="post-metadata">

**Author:** ![dhrubo](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@dhrubo](https://forum.opensearch.org/u/dhrubo)\
**Post date:** [March 30, 2026, 8:33pm UTC](https://forum.opensearch.org/t/registered-agent-using-gemini-returns-api-key-not-valid/27957/2 "2026-03-30T20:33:32Z")

</div>

Two issues in your registration:

1. Credential key name: The Gemini model provider expects gemini\_api\_key, not api\_key. Internally, the connector sets the HTTP header x-goog-api-key: ${credential.gemini\_api\_key} ([source]([https://github.com/opensearch-](https://github.com/opensearch-)  
project/ml-commons/blob/main/common/src/main/java/org/opensearch/ml/common/agent/GeminiV1BetaGenerateContentModelProvider.java#L91)). Since your key is named api\_key, the substitution produces an empty value, and Google  
rejects it with “API key not valid.”

2. Field name: Should be credential (singular), not credentials.

Change your registration to:

json  
“model”: {  
“model\_id”: “gemini-2.5-pro”,  
“model\_provider”: “gemini/v1beta/generatecontent”,  
“credential”: {  
“gemini\_api\_key”: “your-actual-api-key”  
},  
“parameters”: {  
“system\_prompt”: “You are an expert data analyst with access to OpenSearch indices”  
}  
}

References:

- [RestGeminiFunctionCallingIT.java](https://github.com/opensearch-project/ml-commons/blob/main/plugin/src/test/java/org/opensearch/ml/rest/RestGeminiFunctionCallingIT.java) — working integration test examples

---

<div class="post-metadata">

**Author:** ![gstorme](https://avatars.discourse-cdn.com/v4/letter/g/91b2a8/32.png) [@gstorme](https://forum.opensearch.org/u/gstorme)\
**Post date:** [March 30, 2026, 9:02pm UTC](https://forum.opensearch.org/t/registered-agent-using-gemini-returns-api-key-not-valid/27957/3 "2026-03-30T21:02:43Z")

</div>

Thanks, this works.  
For reference, a PUT with the updated fields didn’t resolve it, I had to delete the agent, and register it again with the correct parameters.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [May 29, 2026, 9:03pm UTC](https://forum.opensearch.org/t/registered-agent-using-gemini-returns-api-key-not-valid/27957/4 "2026-05-29T21:03:35Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
