Metadata-Version: 2.4
Name: meta-sam-parser
Version: 0.0.4
Summary: Native Python parsing primitives for the SAM 3 segmentation protocol
Project-URL: Homepage, https://github.com/meta-models/meta-sam
Project-URL: Repository, https://github.com/meta-models/meta-sam
Project-URL: Issues, https://github.com/meta-models/meta-sam/issues
Author: Meta Platforms, Inc.
License: SAM License
        Last Updated: November 19, 2025
        
        “Agreement” means the terms and conditions for use, reproduction, distribution and modification of the SAM Materials set forth herein.
        
        
        “SAM Materials” means, collectively, Documentation and the models, software and algorithms, including machine-learning model code, trained model weights, inference-enabling code, training-enabling code, fine-tuning enabling code, and other elements of the foregoing distributed by Meta and made available under this Agreement.
        
        “Documentation” means the specifications, manuals and documentation accompanying
        SAM Materials distributed by Meta.
        
        
        “Licensee” or “you” means you, or your employer or any other person or entity (if you are entering into this Agreement on such person or entity’s behalf), of the age required under applicable laws, rules or regulations to provide legal consent and that has legal authority to bind your employer or such other person or entity if you are entering in this Agreement on their behalf.
        
        
        “Meta” or “we” means Meta Platforms Ireland Limited (if you are located in or, if you are an entity, your principal place of business is in the EEA or Switzerland) or Meta Platforms, Inc. (if you are located outside of the EEA or Switzerland).
        
        
        “Sanctions” means any economic or trade sanctions or restrictions administered or enforced by the United States (including the Office of Foreign Assets Control of the U.S. Department of the Treasury (“OFAC”), the U.S. Department of State and the U.S. Department of Commerce), the United Nations, the European Union, or the United Kingdom.
        
        
        “Trade Controls” means any of the following: Sanctions and applicable export and import controls.
        
        By using or distributing any portion or element of the SAM Materials, you agree to be bound by this Agreement.
        
        
        1. License Rights and Redistribution.
        
        
        a. Grant of Rights. You are granted a non-exclusive, worldwide, non-transferable and royalty-free limited license under Meta’s intellectual property or other rights owned by Meta embodied in the SAM Materials to use, reproduce, distribute, copy, create derivative works of, and make modifications to the SAM Materials.
        
        b. Redistribution and Use.
        i. Distribution of SAM Materials, and any derivative works thereof, are subject to the terms of this Agreement. If you distribute or make the SAM Materials, or any derivative works thereof, available to a third party, you may only do so under the terms of this Agreement and you shall provide a copy of this Agreement with any such SAM Materials.
        
        
        ii.  If you submit for publication the results of research you perform on, using, or otherwise in connection with SAM Materials, you must acknowledge the use of SAM Materials in your publication.
        
        
        iii. Your use of the SAM Materials must comply with applicable laws and regulations, including Trade Control Laws and applicable privacy and data protection laws.
        iv. Your use of the SAM Materials will not involve or encourage others to reverse engineer, decompile or discover the underlying components of the SAM Materials.
        v. You are not the target of Trade Controls and your use of SAM Materials must comply with Trade Controls. You agree not to use, or permit others to use, SAM Materials for any activities subject to the International Traffic in Arms Regulations (ITAR) or end uses prohibited by Trade Controls, including those related to military or warfare purposes, nuclear industries or applications, espionage, or the development or use of guns or illegal weapons.
        2. User Support. Your use of the SAM Materials is done at your own discretion; Meta does not process any information nor provide any service in relation to such use.  Meta is under no obligation to provide any support services for the SAM Materials. Any support provided is “as is”, “with all faults”, and without warranty of any kind.
        
        
        3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE SAM MATERIALS AND ANY OUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, AND META DISCLAIMS ALL WARRANTIES OF ANY KIND, BOTH EXPRESS AND IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE SAM MATERIALS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE SAM MATERIALS AND ANY OUTPUT AND RESULTS.
        
        4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY DIRECT OR INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF META OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING.
        
        5. Intellectual Property.
        
        
        a. Subject to Meta’s ownership of SAM Materials and derivatives made by or for Meta, with respect to any derivative works and modifications of the SAM Materials that are made by you, as between you and Meta, you are and will be the owner of such derivative works and modifications.
        
        b. If you institute litigation or other proceedings against Meta or any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the SAM Materials, outputs or results, or any portion of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable by you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold harmless Meta from and against any claim by any third party arising out of or related to your use or distribution of the SAM Materials.
        
        6. Term and Termination. The term of this Agreement will commence upon your acceptance of this Agreement or access to the SAM Materials and will continue in full force and effect until terminated in accordance with the terms and conditions herein. Meta may terminate this Agreement if you are in breach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete and cease use of the SAM Materials. Sections 3, 4 and 7 shall survive the termination of this Agreement.
        
        7. Governing Law and Jurisdiction. This Agreement will be governed and construed under the laws of the State of California without regard to choice of law principles, and the UN Convention on Contracts for the International Sale of Goods does not apply to this Agreement. The courts of California shall have exclusive jurisdiction of any dispute arising out of this Agreement.
        
        
        8. Modifications and Amendments. Meta may modify this Agreement from time to time; provided that they are similar in spirit to the current version of the Agreement, but may differ in detail to address new problems or concerns. All such changes will be effective immediately. Your continued use of the SAM Materials after any modification to this Agreement constitutes your agreement to such modification. Except as provided in this Agreement, no modification or addition to any provision of this Agreement will be binding unless it is in writing and signed by an authorized representative of both you and Meta.
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: build==1.3.0; extra == 'dev'
Requires-Dist: hatchling==1.27.0; extra == 'dev'
Requires-Dist: jsonschema==4.25.1; extra == 'dev'
Requires-Dist: mypy==1.18.2; extra == 'dev'
Requires-Dist: pytest-cov==7.0.0; extra == 'dev'
Requires-Dist: pytest==9.0.3; extra == 'dev'
Requires-Dist: ruff==0.13.1; extra == 'dev'
Description-Content-Type: text/markdown

# meta-sam-parser

`meta-sam-parser` is the dependency-free native Python implementation of the
language-neutral SAM 3 segmentation protocol in the repository root. It provides
the strict complete-mask raster, COCO RLE, and SVG path conversions, incremental image/video line parser, and async
Responses API stream adapter.

## Install and use

Install the package from this directory with any standard Python installer:

```sh
python -m pip install .
```

## Direct parser API

Import supported APIs only from the package root. Deep imports are unsupported;
underscore-prefixed modules are implementation details and may change without notice.
Format factories are zero-argument and create isolated parser state. The example
feeds the parser one frame of SAM 3.1 output exactly as the API emits it - one line
per frame, `<Nf>` then comma-separated `id<|box;...|><|mask;...|>` records:

```python
from meta_sam_parser import CompletedOutcome, video_segmentation_format

output_text = (
    "<0f>0<|box;x1=211;y1=228;x2=270;y2=254;w=320;h=334|>"
    "<|mask;x=0;y=0;data=27,60,~!!!!M!0c[0o91w?q1!pIH4pPRVp2B3'7`e.ioeAf6-k/#Xd8%dX9x(|>"
    ",1<|box;x1=155;y1=228;x2=202;y2=254;w=320;h=334|>"
    "<|mask;x=0;y=0;data=27,48,~!!!!J!0c[q=Pj_zs=*4C4(/./x#:/`S_GnD`=o3?X{emCgO$y@|>\n"
)

format_ = video_segmentation_format()
parser = format_.create_parser()

# Chunks may split anywhere, including inside a mask payload.
split = output_text.index(",1<|box")
for snapshot in parser.push(output_text[:split]):
    print(snapshot.revision, len(snapshot.records))
for snapshot in parser.push(output_text[split:]):
    print(snapshot.revision, len(snapshot.records))  # 1 4

finished = parser.finish(CompletedOutcome())
for snapshot in finished.events:
    print(snapshot.revision, len(snapshot.records))
result = finished.result
# Two box records and two mask records, object IDs "0" and "1", frame 0.
```

`push()` accepts arbitrarily split text chunks and returns zero or one cumulative
snapshot. `finish()` parses a final unterminated line and returns immutable final
events plus a result. Pass `IncompleteOutcome(reason="response", detail=...)` for
an explicitly incomplete response or `IncompleteOutcome(reason="eof")` when the
source ends without a terminal response event.

The same formats are normally passed to `parse_responses_stream()` so the adapter
can own lane validation, source cleanup, and terminal outcomes.

## Responses API streams

`parse_responses_stream(source, format)` accepts an async iterable of official
OpenAI Python SDK event objects, mappings with the same wire fields, or a mixture
of both. It has no runtime dependency on the OpenAI SDK. Field names stay in the
SDK/wire snake-case form (`item_id`, `output_index`, and `content_index`). An
output-text lane is finalized by either `response.output_text.done` or a
`response.content_part.done` event whose part type is `output_text`. The live SAM
Model API emits only the latter. If a stream emits both finalizers, their text
must match.

### Iterator-first

Use the parsed stream as both an async iterable and an async context manager. The
context manager is important when the loop may exit early because Python does not
implicitly call `aclose()` on arbitrary async iterators.

```python
from meta_sam_parser import parse_responses_stream, video_segmentation_format

async def consume(response_events):
    parsed = parse_responses_stream(response_events, video_segmentation_format())
    async with parsed:
        async for snapshot in parsed:
            print(snapshot.revision, len(snapshot.records))

    result = await parsed.final_result()
    return result
```

The first iterator request selects iterator mode. Calling `final_result()` selects
final-only mode synchronously, before its returned awaitable is awaited, so it
cannot race a later iterator claim. Pulls are serialized, snapshots are produced
on demand, and repeated `final_result()` calls await the same internal terminal
future. In iterator-first mode, requesting the final result does not consume the
remaining source: iteration must still reach completion. Exit early only through
`async with` or `aclose()`, which closes the source and makes the final result
raise `ResponsesStreamAbortedError`. Requesting another iterator raises
`ResponsesStreamConsumedError`.

### Final-only

Calling `final_result()` before requesting an iterator selects final-only mode.
The adapter drains and parses the source while suppressing intermediate snapshots:

```python
result = await parse_responses_stream(
    response_events,
    video_segmentation_format(),
).final_result()
```

### Early exit

Leaving an async context before a terminal response closes a created upstream
iterator once and makes `final_result()` raise `ResponsesStreamAbortedError`:

```python
parsed = parse_responses_stream(response_events, video_segmentation_format())
async with parsed:
    async for snapshot in parsed:
        if snapshot.records:
            break

# Raises ResponsesStreamAbortedError.
await parsed.final_result()
```

### Explicit ownership

Code that does not use `async with` must close the parsed stream explicitly:

```python
parsed = parse_responses_stream(response_events, video_segmentation_format())
try:
    iterator = aiter(parsed)
    first_snapshot = await anext(iterator)
    use(first_snapshot)
finally:
    await parsed.aclose()
```

`aclose()` is idempotent and safe while a source read is pending: it cancels and
waits for that read before closing the source owner exactly once. Both synchronous
and asynchronous close methods are supported, including closing an unstarted
source that owns transport resources. Cancellation of a pull, `final_result()`, or
`aclose()` propagates `asyncio.CancelledError` unchanged; cleanup continues on a
best-effort basis, and a later `final_result()` reports
`ResponsesStreamAbortedError` unless source cleanup itself fails. Parser and source
failures are exception-chained through `__cause__`.

## Immutable public model

All public values are frozen, slotted dataclasses. Observable collections are
tuples. The package root exports:

- Geometry, masks, and aliases: `FrameReference`, `SegmentationMaskBounds`,
  `SegmentationMask`, `SegmentationMaskEncoding`, `SegmentationMaskIdentity`, `SegmentationMedia`,
  `DiagnosticSeverity`, and `IncompleteReason`.
- Records: `SegmentationTextRecord`, `SegmentationBoxRecord`,
  `SegmentationMaskRecord`, and `SegmentationRecord`.
- Views: `SegmentationDiagnostic`, `ImageSegmentationSnapshot`,
  `VideoSegmentationSnapshot`, `SegmentationSnapshot`,
  `ImageSegmentationResult`, `VideoSegmentationResult`, and
  `SegmentationResult`.
- Outcomes and parser contracts: `CompletedOutcome`, `IncompleteOutcome`,
  `ResponseStreamOutcome`, `ParserFinish`, `ResponseFormatParser`, and
  `ResponseFormat`.
- Format factories: `image_segmentation_format` and
  `video_segmentation_format`.
- Stream lifecycle: `ParsedResponsesStream`, `parse_responses_stream`,
  `ResponsesEvent`, `ResponsesEventLike`, `OutputTextLane`, and
  `ResponseSourceOperation`.
- Errors: `ResponsesStreamError`, `ResponsesStreamConsumedError`,
  `ResponsesStreamAbortedError`, `ResponsesStreamFailedError`,
  `ResponsesStreamEventError`, `ResponsesStreamLaneError`,
  `ResponsesStreamRefusalError`, `ResponsesStreamParserError`,
  `ResponsesStreamSourceError` and `InvalidSegmentationMaskError`.
- Conversions: `decode_mask_to_raster`, `decode_mask_to_rle`,
  `decode_mask_to_svg_path`, and the frozen, slotted `RLEObject`.

Fields use snake case. A mask identity is the immutable tuple of media,
`frame_index`, and `object_id`; each later accepted mask for that identity gets
the next revision.

## Parsing behavior

SAM 3.1 returns segmentation as special-token text in one `output_text` lane, one
line per frame:

```text
<Nf>id<|box;x1=..;y1=..;x2=..;y2=..;w=<frameW>;h=<frameH>|><|mask;x=0;y=0;data=<H>,<W>,<enc>payload|>,id<|box;...|><|mask;...|>
```

`<Nf>` is the zero-based frame index; frames without a visible object emit no
line, so indices can skip. Each comma-separated record is a bare integer object
id - stable for an object across the frames of one response and not a dense
sequence - followed by one box and one mask. The parser retains the id as a string
in `object_id`. Box corners and the `w`/`h` frame size are source pixels; the
inclusive wire `x2`/`y2` become half-open `right`/`bottom`. The mask tuple is
`height,width,payload`, where the payload's first character selects the encoding:
`~` for `lossless` (the API default) or `!` for `one_bit`. The payload is base85,
not base64: after the marker its digits are printable ASCII `!` through `{` minus
the wire delimiters `" \ , ; < > |`, so it contains `*`, `$`, brackets, and
backticks, a base64 regex will not match it, and only the first character after
`H,W,` is the marker (`!` is also digit zero). Pass it through unchanged. Each record becomes one `SegmentationBoxRecord` followed by
one `SegmentationMaskRecord` whose `bounds` is the same half-open box. Image streams require frame zero and omit the frame from
normalized records; video records retain `frame.frame_index`. Every mask is
strictly decoded before insertion; an empty lane is a valid completed response
with no records.
The grammar is specified in the
[SAM 3 protocol](https://github.com/meta-models/meta-sam/blob/main/protocol/sam3.md).

Plain text remains an ordered text record. Malformed structured-looking lines
produce diagnostics and parsing continues. `raw_output` preserves every input
character exactly. Newline, CRLF, blank-line, and final unterminated-line behavior
matches the TypeScript parser. JavaScript safe-integer, ASCII token grammar, and
observable numeric parsing boundaries are preserved explicitly.

## Mask conversion

`decode_mask_to_raster()` strictly validates a complete `one_bit` or `lossless`
payload and returns immutable row-major `bytes` containing only `0` and `1`.
`decode_mask_to_rle()` returns exact COCO compressed RLE with `(height, width)`
size after transposing that raster to COCO column-major order.
`decode_mask_to_svg_path()` returns the polygonal `M`/`L`/`Z` path,
including multiple subpaths where needed, and returns `""` for an empty mask.
Structural checks cover supported encodings, positive JavaScript-safe dimensions
and area, packed payload shape and alphabet, prefixes, groups, tails, finalization,
and exact decoded length; the decoder imposes no project-defined area or payload
quota ceiling. Non-memory decoding failures are wrapped as
`InvalidSegmentationMaskError` with their cause, while `MemoryError` propagates
unchanged:

```python
from meta_sam_parser import (
    SegmentationMask,
    decode_mask_to_raster,
    decode_mask_to_rle,
    decode_mask_to_svg_path,
)

mask = SegmentationMask(
    encoding="one_bit",
    payload="!!!!!(QO(0lu8?",
    width=5,
    height=5,
)
raster = decode_mask_to_raster(mask)
coco_rle = decode_mask_to_rle(mask)
svg_path = decode_mask_to_svg_path(mask)
```

`raster` is immutable `bytes` in row-major order and contains only `0` and `1`.
`one_bit` payloads must pass a unique canonical round trip. `lossless` payloads
must use the strict packed envelope and contain enough arithmetic-coder
finalization to decode the declared raster, but they are not uniqueness-
canonicalized: trailing packed bytes and alternate unused finalization bytes may
encode the same raster and are accepted. The package does not expose a lossless
encoder or promise a canonical lossless spelling. Deep imports are implementation
details and are not supported.

## Python support

The declared range is CPython 3.10 and newer. Python 3.10 is the floor because
the public immutable types use standard-library slotted dataclasses and the
codebase uses Python 3.10 type syntax. There is no upper bound because the
runtime is pure Python, has no dependencies, and does not use CPython internals.
CI exercises Python 3.10 through 3.14.

## Development

Create and activate a virtual environment, then install the pinned development
toolchain:

```sh
python -m pip install -e '.[dev]'
python -m ruff format --check .
python -m ruff check .
python -m mypy
python -m pytest
python scripts/build_artifacts.py
python scripts/audit_distribution.py
```

The package audit verifies exact wheel and sdist allowlists, metadata, the typed
root API, archive safety, reproducible bytes, and isolated wheel and sdist
consumers. Each consumer runs `pip check`, runtime lifecycle cases, and strict
static typing against the installed distribution. The wheel consumer also
installs the official OpenAI Python SDK version pinned in
`requirements-openai.txt`, statically accepts `AsyncStream[ResponseStreamEvent]`,
and passes its attribute-object events through the installed parser while verifying
transport cleanup. OpenAI is a test-only consumer dependency and is not a runtime
package dependency.

## Releasing

`meta-sam-parser` is published to [PyPI](https://pypi.org/project/meta-sam-parser/)
by the `release PyPI distribution` workflow
(`.github/workflows/release-pypi.yml`). A release is one commit and one tag:

1. Bump `version` in `pyproject.toml`, run `node scripts/sync-compatibility` from
   the repository root so the compatibility matrix records the new version, and
   merge that change to `main`.
2. Push the tag `meta-sam-parser@<version>` at that merge commit on `main`. The
   workflow refuses a tag that does not match the manifest version, a tag whose
   commit is not on `main`, and a tag older than the latest `main` commit touching
   `python/`, `conformance/`, or `protocol/`, so a tag created before later fixes
   merged cannot publish stale source.
3. The workflow builds the reproducible wheel and sdist, runs the artifact and
   clean-consumer audits and the cross-language conformance suite, and then waits
   for approval in the `pypi` GitHub environment before uploading through PyPI
   trusted publishing (OIDC) with attestations. No PyPI credential is stored in the
   repository. It then creates the GitHub release for the tag.

A manual dispatch from `main` rehearses the same build and audits against
TestPyPI from the `testpypi` environment and never publishes to PyPI.

Build and audit the same artifacts locally from `python/`:

```sh
python -m pip install -e '.[dev]'
python scripts/build_artifacts.py
python scripts/audit_distribution.py
```

The build command replaces `dist/` with exactly one wheel and one sdist for the
version declared in `pyproject.toml`. The audit must pass against those exact
files; it does not upload, publish, or read credentials.

The Python conformance tests execute all 29 shared cases through
`parse_responses_stream()`, including stream lifecycle failures, completed and
incomplete outcomes, diagnostics, and masks. From the repository root, `node
scripts/validate-conformance` runs the same exact normalized cases in both
languages, while `node scripts/validate` runs complete validation and builds both
distributions.

## License

`meta-sam-parser` is licensed under the [SAM License](LICENSE).
