Skip to content

Commit

Permalink
Fix KeyError "type" for untagged content encoding (#1439)
Browse files Browse the repository at this point in the history
* Update CHANGELOG.md

* Fix KeyError "type" for untagged content encoding

* Minify diff

---------

Co-authored-by: jjallaire <[email protected]>
  • Loading branch information
alexb-palisaderesearch and jjallaire authored Mar 8, 2025
1 parent db9fa8f commit 69f0742
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Improved async implementation using AnyIO (can now optionally run Trio rather than asyncio as the [async backend](https://inspect.ai-safety-institute.org.uk/parallelism.html#async-backends)).
- Agent Bridge: Correct handling for `tool_choice` option.
- Model API: `ChatMessage` now includes an `id` field (defaults to auto-generated uuid).
- OpenAI: More flexible parsing of content parts (some providers omit the "type" field); support for "reasoning" content parts.
- Mistral: Update to new Mistral API (v1.5.1 of `mistralai` is now required).
- Logging: Inspect no longer sets the global log level nor does it allow its own messages to propagate to the global handler (eliminating the possiblity of duplicate display). This should improve compatibility with applications that have their own custom logging configured.
- Tasks: For filesystem based tasks, no longer switch to the task file's directory during execution (directory switching still occurs during task loading). Specify `@task(chdir=True)` to preserve the previous behavior.
Expand Down
5 changes: 5 additions & 0 deletions src/inspect_ai/model/_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ def content_from_openai(
content: ChatCompletionContentPartParam | ChatCompletionContentPartRefusalParam,
parse_reasoning: bool = False,
) -> list[Content]:
# Some providers omit the type tag and use "object-with-a-single-field" encoding
if "type" not in content and len(content) == 1:
content["type"] = list(content.keys())[0]
if content["type"] == "text":
text = content["text"]
if parse_reasoning:
Expand All @@ -413,6 +416,8 @@ def content_from_openai(
return [ContentText(text=text)]
else:
return [ContentText(text=text)]
elif content["type"] == "reasoning":
return [ContentReasoning(reasoning=content["reasoning"])]
elif content["type"] == "image_url":
return [
ContentImage(
Expand Down

0 comments on commit 69f0742

Please sign in to comment.