messages.create() takes a JSON schema dict but messages.stream() takes a Pydantic model
Problem
Just ran into this slight inconsistency in the API relating to structured content support. Here's the `messages.create()` idea of `output_format=`: https://github.com/anthropics/anthropic-sdk-python/blob/d9aea38e754d55f8f0875fdf19ee44f78ca7b845/src/anthropic/resources/beta/messages/messages.py#L106 Which uses: https://github.com/anthropics/anthropic-sdk-python/blob/d9aea38e754d55f8f0875fdf19ee44f78ca7b845/src/anthropic/types/beta/beta_json_output_format_param.py#L11-L15 So it accepts a Python dictionary that represents a JSON schema. But the `messages.stream()` method: https://github.com/anthropics/anthropic-sdk-python/blob/d9aea38e754d55f8f0875fdf19ee44f78ca7b845/src/anthropic/resources/beta/messages/messages.py#L1351 Later does this: https://github.com/anthropics/anthropic-sdk-python/blob/d9aea38e754d55f8f0875fdf19ee44f78ca7b845/src/anthropic/resources/beta/messages/messages.py#L1385-L1386 `TypeAdapter` is a Pydantic concept, so this has the effect of only allowing Pydantic types to be passed to `messages.stream()`. It's confusing and inconsistent that `messages.create()` requires a JSON schema dictionary but `messages.stream()` instead requires a Pydantic model.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Standardize API Input Types for messages.create() and messages.stream()
The inconsistency arises because the `messages.create()` method accepts a JSON schema dictionary while `messages.stream()` requires a Pydantic model. This design choice creates confusion for users who expect a uniform interface for structured content support across both methods.
Awaiting Verification
Be the first to verify this fix
- 1
Modify messages.stream() to Accept JSON Schema
Update the `messages.stream()` method to accept a JSON schema dictionary similar to `messages.create()`. This will align the input types of both methods and reduce confusion.
pythondef stream(self, json_schema: dict): # Convert JSON schema to Pydantic model internally pydantic_model = self.convert_to_pydantic(json_schema) return self._stream_with_model(pydantic_model) - 2
Implement Conversion Function
Create a utility function `convert_to_pydantic` that takes a JSON schema dictionary and converts it into a Pydantic model. This ensures that the internal workings of `messages.stream()` remain compatible with the expected input.
pythondef convert_to_pydantic(json_schema: dict): # Logic to convert JSON schema to Pydantic model return PydanticModel.parse_obj(json_schema) - 3
Update Documentation
Revise the API documentation to reflect the changes made to `messages.stream()`. Clearly state that both methods now accept a JSON schema dictionary, and provide examples for clarity.
- 4
Run Unit Tests
Create and run unit tests to ensure that both `messages.create()` and `messages.stream()` function correctly with JSON schema inputs. Verify that the expected outputs are produced for various valid and invalid inputs.
pythondef test_stream_with_json_schema(): json_schema = {'type': 'object', 'properties': {'message': {'type': 'string'}}} response = messages.stream(json_schema) assert response is not None - 5
Gather User Feedback
After deploying the changes, gather feedback from users regarding the new input handling. Ensure that the changes have resolved the confusion and improved the user experience.
Validation
Confirm that both `messages.create()` and `messages.stream()` accept and correctly process JSON schema dictionaries by running integration tests and checking the outputs. User feedback should also indicate improved clarity.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep