Skip to content

Optional Fields in Agent Output Cause JSON Schema Error with AWS Bedrock #586

Closed
@samDobsonDev

Description

@samDobsonDev

Summary:
When using the OpenAI Agents SDK with a custom agent output schema that includes Optional fields (defined using Pydantic), the request to AWS Bedrock fails with a 400 Bad Request error. The error indicates that the generated JSON schema is invalid according to JSON Schema Draft 2020-12. If I remove the Optional fields, the request succeeds, but then the model is forced to always populate those fields, which is not the desired behaviour.

Environment:
Agents SDK Version: 0.0.12
Model: Claude 3.5 Sonnet via AWS Bedrock with LiteLLM

Steps to Reproduce

  1. Define an output schema using Pydantic, where several fields are marked as Optional, such as this example:
class Link(BaseModel):
    """Represents a clickable link."""
    label: str = Field(..., description="Text that appears in the clickable bubble. MAX 20 CHARACTERS") # Required
    url: str = Field(..., description="URL the link points to") # Required

class Body(BaseModel):
    """Represents the main body of text with an optional link."""
    text: str = Field(..., description="Main body of text. This is required.")  # Required
    link: Optional[Link] = Field(None, description="A clickable link that appears AFTER the main body of text.")  # Optional

class QuickReply(BaseModel):
    """Represents a quick reply button."""
    title: str = Field(..., description="Text that appears to the user in the bubble and also the text that is sent to the LLM when clicked. 1-2 WORDS AND MAX 20 CHARACTERS")  # Required

class Button(BaseModel):
    """Represents a button in carousel elements."""
    label: str = Field(..., description="Text that appears on the button. MAX 20 CHARACTERS") # Required
    url: str = Field(..., description="URL the button points to") # Required

class CarouselElement(BaseModel):
    """Represents an element in a carousel."""
    title: str = Field(..., description="Title of the carousel element") # Required
    subtitle: Optional[str] = Field(None, description="Subtitle of the carousel element") # Optional
    imageUrl: Optional[str] = Field(None, description="URL of the image for the carousel element") # Optional
    buttons: List[Button] = Field(default_factory=list, description="List of buttons for this carousel element") # Optional (default empty list)

class Carousel(BaseModel):
    """Represents a carousel containing multiple elements."""
    elements: List[CarouselElement] = Field(..., description="List of carousel elements") # Required

class RichResponse(BaseModel):
    """Agent response container with rich messaging components
    body: Main text content with optional link (REQUIRED)
    quick_replies: Post-message action buttons (OPTIONAL)
    carousel: Carousel containing multiple elements (OPTIONAL)
    """
    body: Body = Field(..., description="Main body of text with optional link. This is required.")  # Required
    quick_replies: Optional[List[QuickReply]] = Field(default_factory=list, description="List of quick reply buttons. These appear AFTER the main body of text. These are optional.")  # Optional (default empty list)
    carousel: Optional[Carousel] = Field(default_factory=list, description="A carousel containing multiple elements, These are optional.")  # Optional (default empty list)
  1. Set this schema as the Agent's output.
  2. Run the Agent, triggering a call to any model on AWS Bedrock using LitellmModel
  3. Observe that the following error occurs:BedrockError: {"message":"The model returned the following errors: tools.0.custom.input_schema: JSON schema is invalid. It must match JSON Schema draft 2020-12."}

Expected Behavior

  1. The Agent should accept Optional fields in the output schema without causing a JSON schema validation error.
  2. The model should only populate optional fields when relevant, leaving them absent or null otherwise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-more-infoWaiting for a reply/more info from the author

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions