Skip to content

bug(@langchain/google): codeExecutionResult and executableCode content chunks are incorrectly transformed for Gemini API #10567

@vjancik

Description

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

AIMessage of shape:

[
  {
    "lc": 1,
    "type": "constructor",
    "id": [
      "langchain_core",
      "messages",
      "AIMessage"
    ],
    "kwargs": {
      "lc_serializable": true,
      "lc_kwargs": {
        "lc_serializable": true,
        "lc_kwargs": {
          "lc_serializable": true,
          "lc_kwargs": {
            "content": [
              {
                "type": "text",
                "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\n"
              },
              {
                "type": "executableCode",
                "executableCode": {
                  "language": "PYTHON",
                  "code": "import math\n\nmu = 100\nsigma = 15\n\ndef normal_pdf(x, mu, sigma):\n    coefficient = 1 / (sigma * math.sqrt(2 * math.pi))\n    exponent = -((x - mu)**2) / (2 * sigma**2)\n    return coefficient * math.exp(exponent)\n\n# Calculate PDF for some representative IQ scores\niq_scores = [40, 55, 70, 85, 100, 115, 130, 145, 160]\npdf_values = {score: normal_pdf(score, mu, sigma) for score in iq_scores}\n\nfor score, pdf in pdf_values.items():\n    print(f\"IQ Score: {score}, PDF Value: {pdf:.6f}\")\n"
                }
              },
              {
                "type": "codeExecutionResult",
                "codeExecutionResult": {
                  "outcome": "OUTCOME_OK",
                  "output": "IQ Score: 40, PDF Value: 0.000009\nIQ Score: 55, PDF Value: 0.000295\nIQ Score: 70, PDF Value: 0.003599\nIQ Score: 85, PDF Value: 0.016131\nIQ Score: 100, PDF Value: 0.026596\nIQ Score: 115, PDF Value: 0.016131\nIQ Score: 130, PDF Value: 0.003599\nIQ Score: 145, PDF Value: 0.000295\nIQ Score: 160, PDF Value: 0.000009\n"
                }
              },
              {
                "type": "text",
                "text": "These values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly"
              },
              {
                "type": "text",
                "text": " decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
              }
            ],
            "additional_kwargs": {
              "originalTextContentBlock": {
                "type": "text",
                "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\nThese values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
              },
              "finishReason": "STOP"
            },
            "response_metadata": {
              "model_provider": "google",
              "finishReason": "STOP"
            },
            "tool_call_chunks": [],
            "tool_calls": [],
            "id": "run-019d4675-24e3-7642-a838-591cbe623ac7",
            "usage_metadata": {
              "input_tokens": 184,
              "output_tokens": 717,
              "total_tokens": 1895,
              "input_token_details": {
                "text": 184,
                "cache_read": 0
              },
              "output_token_details": {
                "reasoning": 328
              }
            },
            "invalid_tool_calls": []
          },
          "lc_namespace": [
            "langchain_core",
            "messages"
          ],
          "id": "run-019d4675-24e3-7642-a838-591cbe623ac7",
          "content": [
            {
              "type": "text",
              "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\n"
            },
            {
              "type": "executableCode",
              "executableCode": {
                "language": "PYTHON",
                "code": "import math\n\nmu = 100\nsigma = 15\n\ndef normal_pdf(x, mu, sigma):\n    coefficient = 1 / (sigma * math.sqrt(2 * math.pi))\n    exponent = -((x - mu)**2) / (2 * sigma**2)\n    return coefficient * math.exp(exponent)\n\n# Calculate PDF for some representative IQ scores\niq_scores = [40, 55, 70, 85, 100, 115, 130, 145, 160]\npdf_values = {score: normal_pdf(score, mu, sigma) for score in iq_scores}\n\nfor score, pdf in pdf_values.items():\n    print(f\"IQ Score: {score}, PDF Value: {pdf:.6f}\")\n"
              }
            },
            {
              "type": "codeExecutionResult",
              "codeExecutionResult": {
                "outcome": "OUTCOME_OK",
                "output": "IQ Score: 40, PDF Value: 0.000009\nIQ Score: 55, PDF Value: 0.000295\nIQ Score: 70, PDF Value: 0.003599\nIQ Score: 85, PDF Value: 0.016131\nIQ Score: 100, PDF Value: 0.026596\nIQ Score: 115, PDF Value: 0.016131\nIQ Score: 130, PDF Value: 0.003599\nIQ Score: 145, PDF Value: 0.000295\nIQ Score: 160, PDF Value: 0.000009\n"
              }
            },
            {
              "type": "text",
              "text": "These values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
            }
          ],
          "additional_kwargs": {
            "originalTextContentBlock": {
              "type": "text",
              "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\nThese values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
            },
            "finishReason": "STOP"
          },
          "response_metadata": {
            "model_provider": "google",
            "finishReason": "STOP"
          },
          "type": "ai",
          "tool_calls": [],
          "invalid_tool_calls": [],
          "tool_call_chunks": [],
          "usage_metadata": {
            "input_tokens": 184,
            "output_tokens": 717,
            "total_tokens": 1895,
            "input_token_details": {
              "text": 184,
              "cache_read": 0
            },
            "output_token_details": {
              "reasoning": 328
            }
          }
        },
        "lc_namespace": [
          "langchain_core",
          "messages"
        ],
        "id": "run-019d4675-24e3-7642-a838-591cbe623ac7",
        "content": [
          {
            "type": "text",
            "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\n"
          },
          {
            "type": "executableCode",
            "executableCode": {
              "language": "PYTHON",
              "code": "import math\n\nmu = 100\nsigma = 15\n\ndef normal_pdf(x, mu, sigma):\n    coefficient = 1 / (sigma * math.sqrt(2 * math.pi))\n    exponent = -((x - mu)**2) / (2 * sigma**2)\n    return coefficient * math.exp(exponent)\n\n# Calculate PDF for some representative IQ scores\niq_scores = [40, 55, 70, 85, 100, 115, 130, 145, 160]\npdf_values = {score: normal_pdf(score, mu, sigma) for score in iq_scores}\n\nfor score, pdf in pdf_values.items():\n    print(f\"IQ Score: {score}, PDF Value: {pdf:.6f}\")\n"
            }
          },
          {
            "type": "codeExecutionResult",
            "codeExecutionResult": {
              "outcome": "OUTCOME_OK",
              "output": "IQ Score: 40, PDF Value: 0.000009\nIQ Score: 55, PDF Value: 0.000295\nIQ Score: 70, PDF Value: 0.003599\nIQ Score: 85, PDF Value: 0.016131\nIQ Score: 100, PDF Value: 0.026596\nIQ Score: 115, PDF Value: 0.016131\nIQ Score: 130, PDF Value: 0.003599\nIQ Score: 145, PDF Value: 0.000295\nIQ Score: 160, PDF Value: 0.000009\n"
            }
          },
          {
            "type": "text",
            "text": "These values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
          }
        ],
        "additional_kwargs": {
          "originalTextContentBlock": {
            "type": "text",
            "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\nThese values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
          },
          "finishReason": "STOP"
        },
        "response_metadata": {
          "model_provider": "google",
          "finishReason": "STOP"
        },
        "type": "ai",
        "tool_calls": [],
        "invalid_tool_calls": [],
        "tool_call_chunks": [],
        "usage_metadata": {
          "input_tokens": 184,
          "output_tokens": 717,
          "total_tokens": 1895,
          "input_token_details": {
            "text": 184,
            "cache_read": 0
          },
          "output_token_details": {
            "reasoning": 328
          }
        }
      },
      "lc_namespace": [
        "langchain_core",
        "messages"
      ],
      "id": "run-019d4675-24e3-7642-a838-591cbe623ac7",
      "content": [
        {
          "type": "text",
          "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\n"
        },
        {
          "type": "executableCode",
          "executableCode": {
            "language": "PYTHON",
            "code": "import math\n\nmu = 100\nsigma = 15\n\ndef normal_pdf(x, mu, sigma):\n    coefficient = 1 / (sigma * math.sqrt(2 * math.pi))\n    exponent = -((x - mu)**2) / (2 * sigma**2)\n    return coefficient * math.exp(exponent)\n\n# Calculate PDF for some representative IQ scores\niq_scores = [40, 55, 70, 85, 100, 115, 130, 145, 160]\npdf_values = {score: normal_pdf(score, mu, sigma) for score in iq_scores}\n\nfor score, pdf in pdf_values.items():\n    print(f\"IQ Score: {score}, PDF Value: {pdf:.6f}\")\n"
          }
        },
        {
          "type": "codeExecutionResult",
          "codeExecutionResult": {
            "outcome": "OUTCOME_OK",
            "output": "IQ Score: 40, PDF Value: 0.000009\nIQ Score: 55, PDF Value: 0.000295\nIQ Score: 70, PDF Value: 0.003599\nIQ Score: 85, PDF Value: 0.016131\nIQ Score: 100, PDF Value: 0.026596\nIQ Score: 115, PDF Value: 0.016131\nIQ Score: 130, PDF Value: 0.003599\nIQ Score: 145, PDF Value: 0.000295\nIQ Score: 160, PDF Value: 0.000009\n"
          }
        },
        {
          "type": "text",
          "text": "These values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
        }
      ],
      "additional_kwargs": {
        "originalTextContentBlock": {
          "type": "text",
          "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\nThese values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
        },
        "finishReason": "STOP"
      },
      "response_metadata": {
        "model_provider": "google",
        "finishReason": "STOP"
      },
      "type": "ai",
      "tool_calls": [],
      "invalid_tool_calls": [],
      "tool_call_chunks": [],
      "usage_metadata": {
        "input_tokens": 184,
        "output_tokens": 717,
        "total_tokens": 1895,
        "input_token_details": {
          "text": 184,
          "cache_read": 0
        },
        "output_token_details": {
          "reasoning": 328
        }
      }
    }
  }
]

transforms into this generateContent request body:

   {
        "role": "model",
        "parts": [
          {
            "text": "To render the IQ normal distribution, we need to consider its standard parameters: a mean (μ) of 100 and a standard deviation (σ) of 15.\n\nThe normal distribution for IQ scores describes how these scores are typically spread across a population. Most people will have IQ scores clustering around the mean of 100, with fewer people having very high or very low scores.\n\nHere's how the probability density function (PDF) looks at key points for an IQ distribution:\n\n"
          },
          {
            "type": "executableCode",
            "executableCode": {
              "language": "PYTHON",
              "code": "import math\n\nmu = 100\nsigma = 15\n\ndef normal_pdf(x, mu, sigma):\n    coefficient = 1 / (sigma * math.sqrt(2 * math.pi))\n    exponent = -((x - mu)**2) / (2 * sigma**2)\n    return coefficient * math.exp(exponent)\n\n# Calculate PDF for some representative IQ scores\niq_scores = [40, 55, 70, 85, 100, 115, 130, 145, 160]\npdf_values = {score: normal_pdf(score, mu, sigma) for score in iq_scores}\n\nfor score, pdf in pdf_values.items():\n    print(f\"IQ Score: {score}, PDF Value: {pdf:.6f}\")\n"
            }
          },
          {
            "type": "codeExecutionResult",
            "codeExecutionResult": {
              "outcome": "OUTCOME_OK",
              "output": "IQ Score: 40, PDF Value: 0.000009\nIQ Score: 55, PDF Value: 0.000295\nIQ Score: 70, PDF Value: 0.003599\nIQ Score: 85, PDF Value: 0.016131\nIQ Score: 100, PDF Value: 0.026596\nIQ Score: 115, PDF Value: 0.016131\nIQ Score: 130, PDF Value: 0.003599\nIQ Score: 145, PDF Value: 0.000295\nIQ Score: 160, PDF Value: 0.000009\n"
            }
          },
          {
            "text": "These values show the characteristic \"bell curve\" shape:\n\n*   The highest point of the distribution is at the mean (IQ 100).\n*   The curve is symmetrical around the mean.\n*   As you move further away from the mean (towards very low or very high IQ scores), the probability density rapidly decreases, indicating fewer individuals at those extreme ends.\n\nThis data could be used to plot the classic bell curve for IQ scores."
          }
        ]
      },

Error Message and Stack Trace (if applicable)

which results in an Error from Gemini API:

{
  "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse",
  "status": 400,
  "body": {
    "error": {
      "code": 400,
      "message": "Invalid JSON payload received. Unknown name \"type\" at 'contents[1].parts[1]': Cannot find field.\nInvalid JSON payload received. Unknown name \"type\" at 'contents[1].parts[2]': Cannot find field.",
      "status": "INVALID_ARGUMENT",
      "details": [
        {
          "@type": "type.googleapis.com/google.rpc.BadRequest",
          "fieldViolations": [
            {
              "field": "contents[1].parts[1]",
              "description": "Invalid JSON payload received. Unknown name \"type\" at 'contents[1].parts[1]': Cannot find field."
            },
            {
              "field": "contents[1].parts[2]",
              "description": "Invalid JSON payload received. Unknown name \"type\" at 'contents[1].parts[2]': Cannot find field."
            }
          ]
        }
      ]
    }
  }
}

Description

The type property isn't stripped from these two google-specific content chunk types.

System Info

Package: @langchain/google
Version: 0.1.9
Companion: @langchain/core 1.1.37 (peerDependency enforced)
Runtime: bun 1.3.11
Platform: Ubuntu 25.10 (Linux 6.17.0-14-generic)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions