BoundaryML/baml · error
invalid StreamStateType: %q
Error message
invalid StreamStateType: %q
What it means
StreamStateType is an aliased string enum; MarshalJSON validates the value before serializing. If the StreamStateType value was constructed from an arbitrary string that is not one of the defined constants, marshaling fails with this error instead of emitting an invalid enum value into JSON.
Source
Thrown at engine/language_client_go/baml_go/shared/stream_state.go:40
}
}
// IsValid checks whether the given AliasedEnum value is valid.
func (e StreamStateType) IsValid() bool {
for _, v := range e.Values() {
if e == v {
return true
}
}
return false
}
// MarshalJSON customizes JSON marshaling for AliasedEnum.
func (e StreamStateType) MarshalJSON() ([]byte, error) {
if !e.IsValid() {
return nil, fmt.Errorf("invalid StreamStateType: %q", e)
}
return json.Marshal(string(e))
}
// UnmarshalJSON customizes JSON unmarshaling for AliasedEnum.
func (e *StreamStateType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*e = StreamStateType(s)
if !e.IsValid() {
return fmt.Errorf("invalid StreamStateType: %q", s)
}
return nil
}
type StreamState[T any] struct {View on GitHub (pinned to bd85ce9dee)
Solutions
- Print/log the value and compare against the defined StreamStateType constants; fix the spelling/value.
- Use the exported constants instead of raw string conversions.
- Populate the field via json.Unmarshal (which validates) or a constructor that calls IsValid().
- Check for the zero value ("") when a struct containing StreamStateType is created without initialization.
Example fix
// before
state := shared.StreamStateType("comlete")
data, _ := json.Marshal(state) // error
// after
state := shared.StreamStateRunning // use defined constant
data, _ := json.Marshal(state) Defensive patterns
Strategy: type-guard
Validate before calling
func validStreamStateType(s string) bool {
return shared.StreamStateType(s).IsValid()
} Type guard
func asStreamStateType(s string) (shared.StreamStateType, bool) {
t := shared.StreamStateType(s)
return t, t.IsValid()
} Try / catch
data, err := json.Marshal(state)
if err != nil {
var inv *json.UnsupportedTypeError
_ = errors.As(err, &inv)
return fmt.Errorf("stream state %q invalid: %w", state, err)
} Prevention
- Always use exported StreamStateType constants
- Never convert raw strings to StreamStateType without IsValid()
- Initialize structs containing StreamStateType explicitly
When it happens
Trigger: Creating a StreamStateType via a raw conversion like StreamStateType("runnning") (typo) or reading it from external data without UnmarshalJSON validation, then calling json.Marshal on a struct containing it.
Common situations: Hand-written stream state strings, values loaded from config or another service with different enum spellings, or zero-value StreamStateType("") accidentally left unset before marshaling.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- unexpected stream state
- unexpected cffi.isCFFIFieldTypeLiteral_Literal: %#v
- unexpected type for enum value builder: %T
- failed to get JSON: %w
- invalid media type: '%s'
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/07565381cad90d9c.
Report an issue: GitHub.