openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Pydantic validation failure when LLM sends JSON content in tool call

Open rageshkrishna opened this issue 1 year ago • 3 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [x] This is an issue with the Python library

Describe the bug

I have created a function tool with the following signature:

@function_tool
def write_file(file_path: str, content: bytes) -> str:

My LLM is trying to invoke this with the content set to a JSON string. This causes a Pydantic validation failure and my tool does not get called. I have tried this with both bytes and str, but this makes no difference as you can see from my LM Studio logs below:

<|im_start|>assistant
Now I'll update the package.json file to add a start script:

<tool_call>
<function=write_file>
<parameter=content>
{"name": "hello_world", "version": "1.0.0", "description": "", "main": "app.js", "scripts": {"start": "node app.js", "test": "echo \"Error: no test specified\" && exit 1"}, "keywords": [], "author": "", "license": "ISC", "dependencies": {"express": "^5.1.0"}}
</parameter>
<parameter=file_path>
C:/Temp/try-lightsaber/hello_world/package.json
</parameter>
</function>
</tool_call><|im_end|>
<|im_start|>user
<tool_response>
An error occurred while running the tool. Please try again. Error: Invalid JSON input for tool write_file: 1 validation error for write_file_args
content
  Input should be a valid bytes [type=bytes_type, input_value={'name': 'hello_world', '...: {'express': '^5.1.0'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.12/v/bytes_type
</tool_response>
<|im_end|>

Before changing the content from str to bytes, the error would say Input should be a valid string

It looks like Pydantic doesn't like whatever my LLM is returning and thinks it cannot be either string or bytes. How can I get my function tool to work in this case?

To Reproduce

  1. Create a function tool that accepts a parameter defined as str data type
  2. Get your LLM to invoke that tool with some JSON content
  3. See that the tool call fails

Code snippets


OS

Windows

Python version

Python 3.14.0

Library version

openai==2.6.0, openai-agents==0.4.0

rageshkrishna avatar Oct 21 '25 14:10 rageshkrishna

This is the wrong repo, please open it in https://github.com/openai/openai-agents-python

RobertCraigie avatar Oct 22 '25 16:10 RobertCraigie

Oops, you're right. Sorry about the noise.

rageshkrishna avatar Oct 23 '25 09:10 rageshkrishna

We ran into this same validation issue while testing AI tool-calling workflows. We built StateGuard, a runtime reliability layer for AI workflows, and after reproducing the issue locally we were able to normalize the structured payload before Pydantic validation so the tool call completed successfully without changing the tool signature. Happy to share more if it's useful.

dood1ebyte avatar Jul 19 '26 02:07 dood1ebyte