Dynamic HTTP Request JSON Formatting

Hello!

I have a Workflow that makes an HTTP Request call and GETs an Agent’s metadata.

With that metadata, the agent calls a new HTTP POST call to our server, in which the body of the POST call is filled in by the model. In the description I have provided the model with the schema it should follow. Our server requires the body to be application/json for an HTTP request. The model is able to successfully follow the schema, however, it always creates a Array when it should be creating a Object.

I am not sure, how to go about figuring this out. Since the Body of the HTTP call is filled in by the Agent, I keep running into errors when trying to set the Content Type or Specify Body as an Expression.

I have tried different variations such as

Body Content Type: RAW
Content Type: application/json

OR

Body Content Type: JSON
Specify Body: Using JSON

Here is part of the description, given to the Model:
{
“name”: “AGENT-IMPORTS-WORKFLOW-NAME-FROM-TOOL”,
“description”: “AGENT-CREATES-DESCRIPTION”,

“attributes”: {AGENT-FILLS-IN-FROM-TOOL}
}

Within the Agent System Message, I have written extensive information, guidelines, and proper formatting. The Agent seems to follow everything correctly, I think it is n8n related with JSON Arrays vs Objects.

Is there a better method of doing this? I am fairly new to n8n and experimenting with the platform. I am unable to share the link to my exact workflow due to company policy, can attach screenshots with some omitted information.

I am running n8n locally, version 1.109.2. Any help would be great!

Hi @Anonymous-Monk

Can you try changing the type to json in the $fromaAI like this:

{{ $fromAI('Body', `your description `, 'json') }}

Hi, thank you for such a speedy response.

Trying this yields the same problem, it is able to convert into a JSON but still is in an array and still includes “Body”:

Try explicitly telling the model that you want a JSON object, You can add something like “Return a JSON object” to your prompt.

I think it’s about the prompt/instructions and the model at this point,

here is an example

I will try debugging further. I am using Claude 3.5 Sonnet, we really only get access to Anthropic (3 and 3.5) models through AWS Bedrock. Furthermore, I have the Agent Instructions to follow xml formatting for improved performance and have noted multiple times to return a JSON object. When changing the the key from $fromAI(‘key’, ‘description’, ‘type’) the ‘key’ always shows despite changing the name and it is not possible to remove the key. Is it possible to specifically pull all the data within ‘key’ (in the screenshot case ‘json’)?

[  
 {     
   "key": {          
         "name": "John Doe",
         ...
    }
  }
]

I appreciate all your help!

It doesn’t matter because the data being sent only includes the object values.

Look at this example in the screenshot:

Only the object values are sent..

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