Skip to content

Commit

Permalink
Remove unused content None from openai messages (#402)
Browse files Browse the repository at this point in the history
* Remove unused content None from openai messages

* make test-vcr-fix
  • Loading branch information
jackmpcollins authored Jan 12, 2025
1 parent 7a46010 commit 6343b70
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 184 deletions.
3 changes: 0 additions & 3 deletions src/magentic/chat_model/openai_chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def _(message: AssistantMessage[Any]) -> ChatCompletionMessageParam:
function_schema = FunctionCallFunctionSchema(message.content.function)
return {
"role": OpenaiMessageRole.ASSISTANT.value,
"content": None,
"tool_calls": [
{
"id": message.content._unique_id,
Expand All @@ -149,7 +148,6 @@ def _(message: AssistantMessage[Any]) -> ChatCompletionMessageParam:
if isinstance(message.content, ParallelFunctionCall):
return {
"role": OpenaiMessageRole.ASSISTANT.value,
"content": None,
"tool_calls": [
{
"id": function_call._unique_id,
Expand Down Expand Up @@ -194,7 +192,6 @@ def _(message: AssistantMessage[Any]) -> ChatCompletionMessageParam:
function_schema = function_schema_for_type(type(message.content))
return {
"role": OpenaiMessageRole.ASSISTANT.value,
"content": None,
"tool_calls": [
{
# Can be random because no result will be inserted back into the chat
Expand Down
67 changes: 33 additions & 34 deletions tests/cassettes/test_chatprompt/test_chatprompt_readme_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ interactions:
- request:
body: '{"messages": [{"role": "system", "content": "You are a movie buff."}, {"role":
"user", "content": "What is your favorite quote from Harry Potter?"}, {"role":
"assistant", "content": null, "tool_calls": [{"id": "000000000", "type": "function",
"function": {"name": "return_quote", "arguments": "{\"quote\":\"It does not
do to dwell on dreams and forget to live.\",\"character\":\"Albus Dumbledore\"}"}}]},
{"role": "tool", "tool_call_id": "000000000", "content": "null"}, {"role": "user",
"content": "What is your favorite quote from Iron Man?"}], "model": "gpt-4o",
"parallel_tool_calls": false, "stream": true, "stream_options": {"include_usage":
true}, "tool_choice": {"type": "function", "function": {"name": "return_quote"}},
"tools": [{"type": "function", "function": {"name": "return_quote", "parameters":
{"properties": {"quote": {"title": "Quote", "type": "string"}, "character":
{"title": "Character", "type": "string"}}, "required": ["quote", "character"],
"type": "object"}}}]}'
"assistant", "tool_calls": [{"id": "000000000", "type": "function", "function":
{"name": "return_quote", "arguments": "{\"quote\":\"It does not do to dwell
on dreams and forget to live.\",\"character\":\"Albus Dumbledore\"}"}}]}, {"role":
"tool", "tool_call_id": "000000000", "content": "null"}, {"role": "user", "content":
"What is your favorite quote from Iron Man?"}], "model": "gpt-4o", "parallel_tool_calls":
false, "stream": true, "stream_options": {"include_usage": true}, "tool_choice":
{"type": "function", "function": {"name": "return_quote"}}, "tools": [{"type":
"function", "function": {"name": "return_quote", "parameters": {"properties":
{"quote": {"title": "Quote", "type": "string"}, "character": {"title": "Character",
"type": "string"}}, "required": ["quote", "character"], "type": "object"}}}]}'
headers:
accept:
- application/json
Expand All @@ -21,13 +20,13 @@ interactions:
connection:
- keep-alive
content-length:
- '988'
- '971'
content-type:
- application/json
host:
- api.openai.com
user-agent:
- OpenAI/Python 1.54.4
- OpenAI/Python 1.59.3
x-stainless-arch:
- arm64
x-stainless-async:
Expand All @@ -37,7 +36,7 @@ interactions:
x-stainless-os:
- MacOS
x-stainless-package-version:
- 1.54.4
- 1.59.3
x-stainless-retry-count:
- '0'
x-stainless-runtime:
Expand All @@ -48,56 +47,56 @@ interactions:
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: 'data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_ClHm8dDnFk3Wtq2v0kk5vZvK","type":"function","function":{"name":"return_quote","arguments":""}}],"refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
string: 'data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_DWCoPgZ6a1kfCtlW5ftA8C4D","type":"function","function":{"name":"return_quote","arguments":""}}],"refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"quote"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"quote"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"I"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"I"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
am"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
Iron"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
Man"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":".\",\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":".\",\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"character"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"character"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Tony"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Tony"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
Stark"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"}"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"}"}}]},"logprobs":null,"finish_reason":null}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}],"usage":null}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}],"usage":null}
data: {"id":"chatcmpl-AWvDHV0YBg92gya2uKkeGWxPbIS4N","object":"chat.completion.chunk","created":1732409787,"model":"gpt-4o-2024-08-06","system_fingerprint":"fp_7f6be3efb0","choices":[],"usage":{"prompt_tokens":125,"completion_tokens":13,"total_tokens":138,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}}}
data: {"id":"chatcmpl-AokerhRojPD1bKjnMDI89GBsH4eyu","object":"chat.completion.chunk","created":1736659117,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_b7d65f1a5b","choices":[],"usage":{"prompt_tokens":125,"completion_tokens":13,"total_tokens":138,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}}}
data: [DONE]
Expand All @@ -108,13 +107,13 @@ interactions:
CF-Cache-Status:
- DYNAMIC
CF-RAY:
- 8e7570710c5c17e2-SJC
- 900aafdbe9aa2f2e-LAX
Connection:
- keep-alive
Content-Type:
- text/event-stream; charset=utf-8
Date:
- Sun, 24 Nov 2024 00:56:27 GMT
- Sun, 12 Jan 2025 05:18:37 GMT
Server:
- cloudflare
Transfer-Encoding:
Expand All @@ -126,7 +125,7 @@ interactions:
alt-svc:
- h3=":443"; ma=86400
openai-processing-ms:
- '492'
- '231'
openai-version:
- '2020-10-01'
strict-transport-security:
Expand All @@ -144,7 +143,7 @@ interactions:
x-ratelimit-reset-tokens:
- 100ms
x-request-id:
- req_6b2623fe248b7024f7c51d3d1a0f730c
- req_b749276b21ce7d0ceebaf8ea5f351d25
status:
code: 200
message: OK
Expand Down
Loading

0 comments on commit 6343b70

Please sign in to comment.