Skip to content

Pydantic ValidationError: next-session.close-at Field Required in MarketSession #296

@Perceptron42

Description

@Perceptron42

Description

The MarketSession Pydantic model is raising a validation error because it expects the next-session.close-at field to be present, but the TastyTrade API does not always return this field in the /market-time/sessions/current endpoint response.

May be this is happening due to Christmas holidays?

Environment

  • Library Version: tastytrade==11.0.5
  • Python Version: 3.10
  • Pydantic Version: 2.12
  • Date Observed: December 23, 2025

Error

pydantic_core._pydantic_core.ValidationError: 1 validation error for MarketSession
next-session.close-at
  Field required [type=missing, input_value={'instrument-collection':...-23T22:50:00.000+00:00'}, input_type=dict]

Steps to Reproduce

from tastytrade.session import Session
from tastytrade.market_sessions import get_market_sessions, ExchangeType
session = Session(client_secret, refresh_token)
sessions = get_market_sessions(session, [ExchangeType.CME, ExchangeType.NYSE])

Raw API Response

The actual API response from /market-time/sessions/current shows that next-session does not contain a close-at field:


{
  "items": [
    {
      "close-at": "2025-12-23T22:00:00.000+00:00",
      "instrument-collection": "CME",
      "open-at": "2025-12-22T23:00:00.000+00:00",
      "start-at": "2025-12-22T22:50:00.000+00:00",
      "next-session": {
        "instrument-collection": "CME",
        "open-at": "2025-12-23T23:00:00.000+00:00",
        "session-date": "2025-12-23",
        "start-at": "2025-12-23T22:50:00.000+00:00"
        // ❌ NO "close-at" field
      },
      "previous-session": {
        "close-at": "2025-12-22T22:00:00.000+00:00",
        "instrument-collection": "CME",
        "open-at": "2025-12-21T23:00:00.000+00:00",
        "session-date": "2025-12-22",
        "start-at": "2025-12-21T22:50:00.000+00:00"
      },
      "state": "Open"
    }
  ]
}

Observations
✅ Current session includes close-at
✅ Previous session includes close-at
❌ Next session does NOT include close-at

Expected Behavior

The MarketSession Pydantic model should accept API responses where next-session.close-at is optional/missing, since the API does not always provide this field.

Actual Behavior

Pydantic raises a ValidationError because the model requires next-session.close-at to be present.

Suggested Fix

In the MarketSession model (likely in market_sessions.py), the nested session models should mark the close_at field as optional:

class SessionSnapshot(BaseModel):
    instrument_collection: str = Field(alias="instrument-collection")
    open_at: datetime = Field(alias="open-at")
    session_date: str = Field(alias="session-date")
    start_at: datetime = Field(alias="start-at")
    close_at: Optional[datetime] = Field(default=None, alias="close-at")  # Make optional
    close_at_ext: Optional[datetime] = Field(default=None, alias="close-at-ext")  # Make optional

Thank you for maintaining this excellent library! Let me know if you need any additional information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions