Skip to content

Add Graph large attachment support#472

Merged
benjaminwhtan merged 1 commit intomainfrom
TA-3729/graph-large-attachments
May 6, 2026
Merged

Add Graph large attachment support#472
benjaminwhtan merged 1 commit intomainfrom
TA-3729/graph-large-attachments

Conversation

@benjaminwhtan
Copy link
Copy Markdown
Contributor

@benjaminwhtan benjaminwhtan commented May 5, 2026

License

I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.

Microsoft Graph has a hard limit on inline attachment size (~3 MB via JSON, ~4 MB
via multipart). This PR adds a resumable upload-session API to the Python SDK that
mirrors the existing Node.js implementation, allowing attachments up to 150 MB to
be sent via Graph.

How it works

The upload flow is three steps:

  1. Create a session — nylas.attachments.create_upload_session(identifier,
    request_body) returns a pre-signed URL and an attachment_id.
  2. Upload bytes — the caller PUTs the raw file bytes directly to the pre-signed URL
    (no Nylas auth header needed, outside the SDK).
  3. Complete — nylas.attachments.complete_upload_session(identifier, attachment_id)
    finalises the session. The attachment_id can then be referenced in messages.send()
    or drafts.create().
  res = nylas.attachments.create_upload_session(
      identifier=grant_id,
      request_body={"filename": "report.pdf", "content_type": "application/pdf",
  "size": file_size},
  )
  session = res.data

  requests.put(session.url, data=file_bytes, headers={**session.headers,
  "Content-Length": str(file_size)})

  nylas.attachments.complete_upload_session(identifier=grant_id,
  attachment_id=session.attachment_id)

  nylas.messages.send(
      identifier=grant_id,
      request_body={..., "attachments": [{"id": session.attachment_id}]},
  )

Changes

nylas/models/attachments.py

  • CreateAttachmentUploadSessionRequest — TypedDict for the create request
    (filename, content_type, optional size)
  • AttachmentUploadSession — dataclass for the session response (pre-signed url,
    method, headers, expires_at, max_size, etc.)
  • AttachmentUploadSessionComplete — dataclass for the completion response
    (attachment_id, grant_id, status)
  • AttachmentUploadSessionStatusType — Literal type alias for status values

nylas/resources/attachments.py

  • Added create_upload_session() and complete_upload_session() methods

nylas/resources/messages.py / drafts.py (bug fix)

  • Guarded the attachment["content"] access so that session-reference attachments
    ({"id": "..."}) don't raise KeyError when passed to messages.send() or
    drafts.create()/update()

tests/resources/test_attachments.py

  • 18 new unit tests covering model serialisation/deserialisation and API call
    assertions for both new methods

@benjaminwhtan benjaminwhtan merged commit 6f16e0a into main May 6, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants