Skip to content

Specify blob media type on creation#366

Open
rwb27 wants to merge 3 commits into
mainfrom
specify-blob-media-type-on-creation
Open

Specify blob media type on creation#366
rwb27 wants to merge 3 commits into
mainfrom
specify-blob-media-type-on-creation

Conversation

@rwb27

@rwb27 rwb27 commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

This PR adds a media_type argument to the four class methods that are used to create Blob instances. That allows the media type to be set (and checked against the class media type).

I think the checking is worthwhile for when we have something like an ImageBlob which would have media_type="image/*", and so you can then create an instance with any legal media type starting with image/.

The validation logic was already present in Blob and is tested.

Closes #357
Closes #352

rwb27 added 2 commits June 15, 2026 13:31
This revised docstring should now make sense as the description of the
`Blob` schema in the HTTP API.

This isn't quite as comprehensive as passing through the description of a custom `Blob` subclass, but I think it's enough to close off #357.

Fixes #357
This allows a more specific media type to be specified when creating a
`Blob` instance. This would allow `Blob.from_bytes` to set any media type, or would allow a `Blob` subclass where the first part of the
media type is specified (e.g. `image/*`)
to have any media type starting with `image/`.

This is tested in `test_blob_output.py`
@barecheck

barecheck Bot commented Jun 15, 2026

Copy link
Copy Markdown

Barecheck - Code coverage report

Total: 96.98%

Your code coverage diff: 0.01% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/outputs/blob.py302, 948-949

@rwb27 rwb27 requested a review from julianstirling June 16, 2026 10:49
"""


class MediaTypeMismatchError(ValueError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would be good to clarify this that you mean the specified media string. I read MediaTypeMismatchError and the docstring as the actual media type of the file itself not matching the specifiecation

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've rewritten the docstring accordingly. I feel like this exception is probably appropriate for either case, but currently we only check the supplied media type: the binary data or file doesn't specify a media type.

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.

Blob uses the description from BlobModel in its JSONSchema Allow adding media_type to from_* class methods flor blobs.

2 participants