Skip to content
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

fix: add handling for List[non-object] types #521

Merged
merged 8 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions instructor/anthropic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _add_params(
# TODO: handling of nested params with the same name
properties = model_dict.get("properties", {})
list_found = False
nested_list_found = False

for field_name, details in properties.items():
parameter = ET.SubElement(root, "parameter")
Expand All @@ -69,11 +70,19 @@ def _add_params(
field_type = details.get(
"type", "unknown"
) # Might be better to fail here if there is no type since pydantic models require types

if "array" in field_type and "items" not in details:
raise ValueError("Invalid array item.")

# Adjust type if array
if "array" in field_type or "List" in field_type:
# Check for nested List
if "array" in field_type and "$ref" in details["items"]:
type_element.text = f"List[{details['title']}]"
list_found = True
nested_list_found = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'nested_list_found' is set to True when a nested List type is found but it is not reset to False for non-nested List types. This could potentially cause issues if a non-nested List type follows a nested List type as 'nested_list_found' would still be True. Consider resetting 'nested_list_found' to False for non-nested List types.

# Check for non-nested List
elif "array" in field_type and "type" in details["items"]:
type_element.text = f"List[{details['items']['type']}]"
list_found = True
else:
type_element.text = field_type

Expand All @@ -89,22 +98,13 @@ def _add_params(
_resolve_reference(references, details["$ref"]),
references,
)
elif field_type == "array": # Handling for List[] type
elif field_type == "array" and nested_list_found: # Handling for List[] type
nested_params = ET.SubElement(parameter, "parameters")
list_found |= _add_params(
nested_params,
_resolve_reference(references, details["items"]["$ref"]),
references,
)
elif "array" in field_type: # Handling for optional List[] type
nested_params = ET.SubElement(parameter, "parameters")
list_found |= _add_params(
nested_params,
_resolve_reference(
references, details["anyOf"][0]["items"]["$ref"]
), # CHANGE
references,
)

return list_found

Expand Down
25 changes: 25 additions & 0 deletions tests/anthropic/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ class User(BaseModel):
assert resp.address.house_number == 123
assert resp.address.street_name == "First Avenue"

@pytest.mark.skip
def test_list():
class User(BaseModel):
name: str
age: int
family: List[str]

resp = create(
model="claude-3-opus-20240229", # Fails with claude-3-haiku-20240307
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, we should mention this ins some kind of "anthropic tips" part

max_tokens=1024,
max_retries=0,
messages=[
{
"role": "user",
"content": "Create a user for a model with a name, age, and family members.",
}
],
response_model=User,
)

assert isinstance(resp, User)
assert isinstance(resp.family, List)
for member in resp.family:
assert isinstance(member, str)

@pytest.mark.skip
def test_nested_list():
class Properties(BaseModel):
Expand Down
Loading