GPT-4.1 Tool Response Content Format Issue

GPT-4.1 does not correctly parse tool responses when the content field is structured as a list of objects with a text property containing a JSON string. The model only works as expected when the content field is a single string representing the message.

When the non-working format is used, GPT-4.1 has trouble extracting and using the response data. The model will ignore the tool output and fail to generate a meaningful reply entirely.

Details & Example:

  • Non-working format:

    "content": [
        {
          "text": "{\"message\": {\"ticker\": \"NVDA\", \"current_price\": 179.27, \"previous_close\": 175.51, \"price_change\": 3.76, \"price_change_percent\": 2.14, \"volume\": 166546229, \"high_90d\": 179.89, \"low_90d\": 104.07, \"date\": \"2025-07-30\"}}}",
          "type": "text"
        }
    ]
    
  • Working format:

    "content": "{'message': \"{'ticker': 'NVDA', 'current_price': 179.27, 'previous_close': 175.51, 'price_change': 3.76, 'price_change_percent': 2.14, 'volume': 166546229, 'high_90d': 179.89, 'low_90d': 104.07, 'date': '2025-07-30'}\"}"
    

Impact: the content:[] is more commonly used in many 3rd party SDK. The issue is preventing normal tool call using GPT-4.1.

You don’t discuss if you’re showing us lines of code, or the printed contents of JSON.

Let’s talk in Python code, at a REPL shell or notebook, with the necessary escaping like JSON to use solely double-quotes in the code.

  1. put the string to be sent as tool return into a variable
the_text = "{\"message\": {\"ticker\": \"NVDA\", \"current_price\": 179.27, \"previous_close\": 175.51, \"price_change\": 3.76, \"price_change_percent\": 2.14, \"volume\": 166546229, \"high_90d\": 179.89, \"low_90d\": 104.07, \"date\": \"2025-07-30\"}}"

You can send any ol’ text that the AI can understand.

If you’re unsure that the string is actually clean and JSON-looking for AI understanding:

>>> print(the_text)
{"message": {"ticker": "NVDA", "current_price": 179.27, "previous_close": 175.51, "price_change": 3.76, "price_change_percent": 2.14, "volume": 166546229, "high_90d": 179.89, "low_90d": 104.07, "date": "2025-07-30"}}

(note that yours might have had an extra curly at the end)

(this text really doesn’t need to be enclosed in “message” or any curly-braces. You might process an API’s string into JSON, into just the value of the message.)

  1. put the variable into the construction of an API parameter object:
>>> the_content= [
...     {
...       "text": the_text,
...       "type": "text"
...     }
... ]
  1. create a message item to add to the chat:
>>> the_tool_return_message = {
...     "role":"tool",
...     "content":the_content,
...     "tool_call_id": "12345"
... }

Add this to your python list of dictionary messages (input).

Then - using the printed text of the_text when a prompted AI uses my function:

The JSON API call body that is formed, responding to function call, raw and in full:

{"messages":[{"role":"system","content":[{"type":"text","text":"You are a helpful AI language model."}]},{"role":"user","content":[{"type":"text","text":"Is nVidia stock still trending up?"}]},{"role":"assistant","content":[],"tool_calls":[{"id":"call_aykEepIIlzXEDnZDzkT0IMtR","type":"function","function":{"name":"get_stock_ticker","arguments":"{\"ticker\":\"nVidia\"}"}}]},{"role":"tool","content":[{"type":"text","text":"{\"message\": {\"ticker\": \"NVDA\", \"current_price\": 179.27, \"previous_close\": 175.51, \"price_change\": 3.76, \"price_change_percent\": 2.14, \"volume\": 166546229, \"high_90d\": 179.89, \"low_90d\": 104.07, \"date\": \"2025-07-30\"}}}"}],"tool_call_id":"call_aykEepIIlzXEDnZDzkT0IMtR"}],"temperature":1,"max_completion_tokens":2048,"top_p":1,"frequency_penalty":0,"presence_penalty":0,"model":"gpt-4.1","response_format":{"type":"text"},"tools":[{"type":"function","function":{"name":"get_stock_ticker","description":"Returns the stock ticker symbol based on provided symbol or company name if symbol is unmatched","parameters":{"type":"object","properties":{"ticker":{"type":"string","description":"Stock ticker symbol or company name to search for"}},"required":["ticker"],"additionalProperties":false},"strict":true}}],"parallel_tool_calls":true,"stream":true,"stream_options":{"include_usage":true}}

Thanks for the reply. Let me show you the full messages and code.

from openai import OpenAI
import time


def get_completion(prompt, api_key, model="gpt-4.1"):
    client = OpenAI(
        api_key=api_key
    )

    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_current_stock_data",
                "description": 'Provides current and historical stock price data for a given ticker symbol.',
                "parameters": {
                    "type": "object",
                    "properties": {
                        "ticker": {
                            "description": "Stock ticker symbol (e.g., \"AAPL\" for Apple Inc.). Do not prefix with '$'.",
                            "type": "string",
                        }
                    },
                    "required": ["ticker"]
                }
            },
        }
    ]
    messages = [
        {"role": "system", "content": "You are a stock data assistance."},
        {
            "role": "user",
            "content": f"""
            Follow these steps:
            1. Call the get_current_stock_data with ticker symbol
            2. After the get_current_stock_data responsed, output in the following format: 
            <output_format>
                1. Price Information:
                - Current Price
                - Price Change
                - Volume

                2. Recent Performance:
                - 90-day High/Low
                - Trend Analysis

                3. Key Metrics Summary
            </output_format>

            {prompt}\n""",
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "function": {
                        "name": "get_current_stock_data",
                        "arguments": '{"ticker": "NVDA"}',
                    },
                    "id": "call_BFB2r4NtFBwJj4RxzdhI5etI",
                    "type": "function",
                }
            ],
        },
        {
            "role": "tool",
            "tool_call_id": "call_BFB2r4NtFBwJj4RxzdhI5etI",
            #not working example:
            "content": [         
                {  
                  "type": "text",               
                  "text": "{\"ticker\": \"NVDA\", \"current_price\": 179.27, \"previous_close\": 175.51, \"price_change\": 3.76, \"price_change_percent\": 2.14, \"volume\": 166546229, \"high_90d\": 179.89, \"low_90d\": 104.07, \"date\": \"2025-07-30\"}"                 
                }
            ]
            # working example:
            #"content": "{'ticker': 'NVDA', 'current_price': 179.27, 'previous_close': 175.51, 'price_change': 3.76, 'price_change_percent': 2.14, 'volume': 166546229, 'high_90d': 179.89, 'low_90d': 104.07, 'date': '2025-07-30'}",
        },
    ]
    completion = client.chat.completions.create(
        model=model, messages=messages, tools=tools, temperature=1
    )
    return completion


if __name__ == "__main__":
    api_key = "<api_key>"

    for i in range(1):
        response = get_completion(prompt="Get current stock data for NVDA", api_key=api_key)
        print(f"Run {i+1}:\n{response.choices[0].message.content}")
        time.sleep(4)

Output:

[2025-07-31 18:22:27 - openai._base_client:482 - DEBUG] Request options: {‘method’: ‘post’, ‘url’: ‘/chat/completions’, ‘files’: None, ‘idempotency_key’: ‘stainless-python-retry-501dfe55-d541-4aa0-9182-741966440e24’, ‘json_data’: {‘messages’: [{‘role’: ‘system’, ‘content’: ‘You are a stock data assistance.’}, {‘role’: ‘user’, ‘content’: ‘\n Follow these steps:\n 1. Call the get_current_stock_data with ticker symbol\n 2. After the get_current_stock_data responsed, output in the following format: \n <output_format>\n 1. Price Information:\n - Current Price\n - Price Change\n - Volume\n\n 2. Recent Performance:\n - 90-day High/Low\n - Trend Analysis\n\n 3. Key Metrics Summary\n </output_format>\n\n Get current stock data for NVDA\n’}, {‘role’: ‘assistant’, ‘tool_calls’: [{‘function’: {‘name’: ‘get_current_stock_data’, ‘arguments’: ‘{“ticker”: “NVDA”}’}, ‘id’: ‘call_BFB2r4NtFBwJj4RxzdhI5etI’, ‘type’: ‘function’}]}, {‘role’: ‘tool’, ‘tool_call_id’: ‘call_BFB2r4NtFBwJj4RxzdhI5etI’, ‘content’: [{‘type’: ‘text’, ‘text’: ‘{“ticker”: “NVDA”, “current_price”: 179.27, “previous_close”: 175.51, “price_change”: 3.76, “price_change_percent”: 2.14, “volume”: 166546229, “high_90d”: 179.89, “low_90d”: 104.07, “date”: “2025-07-30”}’}]}], ‘model’: ‘gpt-4.1’, ‘temperature’: 1, ‘tools’: [{‘type’: ‘function’, ‘function’: {‘name’: ‘get_current_stock_data’, ‘description’: ‘PProvides current and historical stock price data for a given ticker symbol.’, ‘parameters’: {‘type’: ‘object’, ‘properties’: {‘ticker’: {‘description’: ‘Stock ticker symbol (e.g., “AAPL” for Apple Inc.). Do not prefix with ‘$’.’, ‘type’: ‘string’}}, ‘required’: [‘ticker’]}}}]}}
[2025-07-31 18:22:27 - openai._base_client:975 - DEBUG] Sending HTTP Request: POST <no_link>
[2025-07-31 18:22:29 - httpx:1025 - INFO] HTTP Request: POST <no_link> “HTTP/1.1 200 OK”
[2025-07-31 18:22:29 - openai._base_client:1013 - DEBUG] HTTP Response: POST <no_link> “200 OK” Headers({‘content-security-policy’: “default-src ‘none’; sandbox”, ‘content-type’: ‘application/json’, ‘strict-transport-security’: ‘max-age=31536000’})
[2025-07-31 18:22:29 - openai._base_client:1021 - DEBUG] request_id: 00000-6b8e42c4-16dc-4916-9378-a71bc3859000
Run 1:

  1. Price Information:
  • Current Price: [Data not returned]
  • Price Change: [Data not returned]
  • Volume: [Data not returned]
  1. Recent Performance:
  • 90-day High/Low: [Data not returned]
  • Trend Analysis: [Data not returned]
  1. Key Metrics Summary

It appears that the requested stock data for NVDA is unavailable at the moment. Please try again shortly or specify if you need different data.

There’s a lot of trailing tab characters around the tool in what I copied from the forum code block. You might want to sanitize your code of AI-generated cruft.

Here is an easy way to place a “seen” JSON into your test code, raw, triple-quoted:

tool_return = r'''{'ticker': 'NVDA', 'current_price': 179.27, 'previous_close': 175.51, 'price_change': 3.76, 'price_change_percent': 2.14, 'volume': 166546229, 'high_90d': 179.89, 'low_90d': 104.07, 'date': '2025-07-30'}'''

Inside - single quotes, double-quotes, communicating the escape sequence, whatever you write essentially gets passed as written (be sure you don’t indent if multi-line)

When working with a real API, data would always be in a natural string.

I ran your example with the exact tool and messages. The output looks different. It doesn’t mention any stock price. However, if I update the prompt to tell it to show the stock data. It just couldn’t do it.

Original Prompt Output:

Run 1:
Nvidia (ticker symbol: NVDA) has generally been trending upward throughout 2024, largely driven by strong earnings, demand for AI chips, and positive investor sentiment. For real-time trend data, including the latest price movement, let me know if you want today’s stock chart or recent performance details!
Run 2:
Nvidia’s stock ticker symbol is NVDA.

Would you like the latest price trend, news, or analysis for NVDA? I can help you track whether it’s still trending up if you specify what data you’re interested in (recent price changes, charts, news, etc.).
Run 3:
Nvidia’s stock ticker symbol is NVDA. Would you like a current summary of its recent stock price trend or specific performance data?
Run 4:
Nvidia’s stock ticker symbol is NVDA.
To provide details about whether Nvidia’s stock is still trending up, could you specify if you’re interested in its price movement today, this week, or over a longer period? Additionally, would you like information on recent news or just the price trend?

Let me know, and I can provide more specific information or analysis!
Run 5:
Nvidia (stock ticker: NVDA) has been one of the most closely watched companies in the stock market, especially due to its involvement in artificial intelligence and GPUs.

Would you like real-time performance data or a recent analysis of Nvidia’s stock trend? Let me know if you want the latest price, recent movement, or expert opinions!
Run 6:
To assess if NVIDIA (ticker: NVDA) stock is still trending up, I can provide recent price and trend information. Would you like current stock data and a summary of recent performance?

Let me know if you’d like a full technical analysis or just the latest price and trend overview.
Run 7:
To provide the most accurate answer about Nvidia’s stock trend, I need to check the recent stock price and performance. Could you specify if you want today’s movement or a recent period (e.g., past week, month, or quarter)?
Run 8:
nVidia’s stock ticker symbol is NVDA. Would you like a detailed update about its recent performance or trends? I can provide the latest news, price data, and analyst sentiment if needed!
Run 9:
Nvidia’s stock ticker symbol is NVDA. To determine whether its stock price is still trending up, you’ll need the most recent price chart or performance data.

Would you like a summary of Nvidia’s recent stock performance, latest news, or analyst opinions? Let me know how you’d like to proceed!
Run 10:
Nvidia’s stock ticker symbol is NVDA. To determine if it is still trending up, you typically need to look at its recent price movements and performance over days, weeks, or months.

Would you like the latest price trend or a recent performance summary for NVDA?

Prompt: Is nVidia stock still trending up? Please provide the stock price and related data.
Output:

Run 1:
Nvidia’s stock ticker is NVDA. To provide the latest price and trend data, I recommend checking real-time financial websites such as Yahoo Finance, Google Finance, or your brokerage for the most current information. As of my latest training data up to June 2024, Nvidia (NVDA) has been trending upward, driven by strong growth in AI and data center markets.

Would you like more details about Nvidia’s recent performance or financial metrics?
Run 2:
NVIDIA (NVDA) is a publicly traded company and its stock has been experiencing notable price movements recently. To determine if NVIDIA’s stock is still trending up, you should check the latest stock price, recent performance data (such as daily change, 52-week high/low, and trading volume), and expert commentary.

Would you like a detailed chart or only the latest price and basic stats? Additionally, do you want any recent news that might have affected NVDA’s trend?
Run 3:
As of my latest data access (June 2024), NVIDIA (ticker symbol: NVDA) has been trending upward, driven by high demand for AI hardware and strong financial performance. However, for the most up-to-date stock price and detailed data, you may want to check a real-time financial news source or brokerage account.

Would you like a summary of recent performance and market sentiment, or do you need specific metrics such as today’s closing price, market capitalization, and 52-week range?
Run 4:
I am retrieving the latest Nvidia (NVDA) stock price and related data for you. Please hold on for a moment while I get the most up-to-date market information.

Would you like additional details such as recent news, analyst ratings, or performance charts as well?
Run 5:
Nvidia’s stock ticker symbol is NVDA. To check if NVDA is still trending up and to view its latest stock price and related data, you’ll need up-to-date information from a financial market source.

Would you like a summary of its recent performance from the past week or month, or specific technical indicators (like moving averages or RSI)? Let me know how detailed you’d like the analysis to be.
Run 6:
I am retrieving the latest price and related data for nVidia (NVDA). Please note that the information provided will reflect the most recent available market data.

Would you like a summary of the recent price trend, or more detailed data such as trading volume, market cap, and historical performance as well?
Run 7:
Nvidia is traded under the ticker symbol “NVDA.”

To get the most recent stock price and related data, please check a financial news site or brokerage account, as real-time prices can fluctuate throughout the trading day. If you would like insights into its recent trends, news, or performance metrics, let me know and I can provide those details!
Run 8:
NVIDIA’s stock ticker symbol is NVDA. To provide current data on its trend and the latest stock price, please specify whether you want real-time data or a historical context (such as the last few months).

I currently cannot provide live market prices, but I can help explain how NVDA has performed recently, or guide you to reliable sources for real-time quotes. Let me know your preference!
Run 9:
To provide accurate information about nVidia’s stock price and current trend, I need the latest data and ticker symbol. Please wait a moment while I retrieve this information for you.

Could you please confirm if you are referring to NVIDIA Corporation (commonly trading under the ticker NVDA)?
Run 10:
It seems I wasn’t able to retrieve the current nVidia stock price and trend directly at this moment.

To get the latest stock price and related data (such as daily price movement, market cap, and trend), I recommend checking a reputable financial site like Yahoo Finance, Google Finance, or your brokerage account.

If you’d like, I can guide you on how to analyze the recent trend for nVidia (NVDA) stock, or let me know if you want another attempt for the stock’s latest figures!

Your code, me being daft and also sending the escaping in the AI language:

  1. Price Information:
  • Current Price: $179.27
  • Price Change: +$3.76 (+2.14%)
  • Volume: 166,546,229
  1. Recent Performance:
  • 90-day High/Low: $179.89 / $104.07
  • Trend Analysis: Strong upward trend, as the current price is near the 90-day high with significant positive movement.
  1. Key Metrics Summary:
    NVIDIA (NVDA) is experiencing strong recent performance, with its stock price pushing towards new highs on elevated trading volume, reflecting robust investor sentiment and momentum.

Your code, a clean JSON:

  1. Price Information:
  • Current Price: $179.27
  • Price Change: +$3.76 (+2.14%)
  • Volume: 166,546,229
  1. Recent Performance:
  • 90-day High/Low: $179.89 / $104.07
  • Trend Analysis: NVDA is near its 90-day high, indicating strong recent upward momentum.
  1. Key Metrics Summary
  • The stock has significantly appreciated over the past quarter, showing robust trading volume and sustained bullish performance.

making it code-like and manipulable first:

import json
...
function_return_dict = {
    'ticker': 'NVDA',
    'current_price': 179.27,
    'previous_close': 175.51,
    'price_change': 3.76,
    'price_change_percent': 2.14,
    'volume': 166546229,
    'high_90d': 179.89,
    'low_90d': 104.07,
    'date': '2025-07-30',
}
function_return_string = json.dumps(function_return_dict, separators=(',', ':'))

then all of us can read the values without scrolling right.

If you are getting downright odd behavior: update the Python SDK that translates python parameters into calls.

Dang have you gotten help yet?

Actually, I am actually using gpt4.1 at azure. The example they are showing doesn’t use any content:[]. Maybe that is the difference there.

With Azure, you also need to include an API version in your request to get newest features and parameters passed to models.

Now you would simply use “preview” to get the newest changing API version to handle and validate your request, instead of getting one with a date and looking every month.

1 Like