-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix missing content
in OpenAI assistant message
#2359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@slmnsh Thanks Salman! Can you please share the exact error you received from OpenAI? And please add a test to ensure we don't accidentally reintroduce this issue in the future. |
@DouweM Added exact error in description and tests |
@slmnsh Thanks. I'm a bit hesitant to merge this because:
So I don't know if this workaround would break more than it fixes. Since this is a real internal error on OpenAI's side, I'd prefer seeing if we can get them to fix it first. Can you please provide me with a minimal reproducible example, ideally directly using their SDK instead of Pydantic AI, that I can try to get to their attention? Edit: Also, for the test, I'd expect something that actually shows the OpenAI error with the current code, and works with the new code -- as is it's basically just restating the fix instead of actually verifying that the fix works against the API! |
I thought you guys missed this but seems like OpenAI issue here. Minimal Example: import base64
import httpx
from openai import OpenAI
from openai.types.chat import ChatCompletionMessageParam
client = OpenAI()
messages: list[ChatCompletionMessageParam] = [
{"content": "This is system prompt", "role": "system"},
{"role": "user", "content": "search abc data"},
{
"role": "assistant",
"tool_calls": [
{
"id": "call_EPt7QoBn5HTqWx6wbqAws8gX",
"type": "function",
"function": {
"name": "search_tool",
"arguments": '{"query_text":"ABC Data"}',
},
}
],
},
{
"role": "tool",
"tool_call_id": "call_EPt7QoBn5HTqWx6wbqAws8gX",
"content": "ABC is a company.",
},
{"role": "assistant", "content": "I found that ABC is a company"},
]
def main():
pdf_url = "https://assets.ctfassets.net/l3l0sjr15nav/29D2yYGKlHNm0fB2YM1uW4/8e638080a0603252b1a50f35ae8762fd/Get_Started_With_Smallpdf.pdf"
pdf = httpx.get(pdf_url)
base64_encoded = base64.b64encode(pdf.content).decode('utf-8')
messages.append({
"role": "user",
"content": [
{"text": "Can you summarize this", "type": "text"},
{
"file": {
"file_data": f'data:application/pdf;base64,{base64_encoded}',
"filename": "filename.pdf",
},
"type": "file",
},
],
})
completion = client.chat.completions.create(model="gpt-4.1", messages=messages)
print(completion.choices[0].message.content)
if __name__ == "__main__":
main() Sorry about tests. I am kinda new to testing 😅 |
@slmnsh Thanks, I've reported it, let's see what they say. |
When LLM responds with
ToolCall
directly without anyTextPart
it's missingcontent
key in Assistant Message. It usually doesn't cause any error but if you try to pass PDF afterwards OpenAI API responds with 500OpenAI Error: