BoundaryML/baml · error · SyntaxError

error while parsing stream_function args: {e}

Error message

error while parsing stream_function args:
{e}

What it means

Raised inside stream_function when the Ruby kwargs hash cannot be converted to JSON for streaming function parameters. Same conversion path as call_function (RubyToJson), but raises a Ruby SyntaxError naming stream_function.

Source

Thrown at engine/language_client_ruby/ext/ruby_ffi/src/lib.rs:174

        retval
    }

    fn stream_function(
        ruby: &Ruby,
        rb_self: &BamlRuntimeFfi,
        function_name: String,
        args: RHash,
        ctx: &RuntimeContextManager,
        type_registry: Option<&types::type_builder::TypeBuilder>,
        client_registry: Option<&types::client_registry::ClientRegistry>,
        collector: RArray,
        env_vars: HashMap<String, String>,
    ) -> Result<FunctionResultStream> {
        let args = match ruby_to_json::RubyToJson::convert_hash_to_json(args) {
            Ok(args) => args.into_iter().collect(),
            Err(e) => {
                return Err(Error::new(
                    ruby.exception_syntax_error(),
                    format!("error while parsing stream_function args:\n{e}"),
                ));
            }
        };

        let mut collectors = Vec::new();
        for i in collector.into_iter() {
            collectors.push(<&Collector>::try_convert(i)?.inner.clone());
        }

        log::debug!("Streaming {function_name} with:\nargs: {args:#?}\nctx ???");

        let retval = match rb_self.inner.stream_function(
            function_name.clone(),
            &args,
            &ctx.inner,
            type_registry.map(|t| &t.inner),

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Inspect {e} to find the offending argument.
  2. Convert all values to JSON-native Ruby types before the call.
  3. Use string keys for nested hashes.
  4. Serialize domain objects explicitly (iso8601 for dates, to_h for structs).

Example fix

// before
Baml.stream_function('Chat', { created_at: Time.now }, ctx, ENV)
// after
Baml.stream_function('Chat', { 'created_at' => Time.now.utc.iso8601 }, ctx, ENV)
Defensive patterns

Strategy: validation

Validate before calling

args.each { |k, v| raise TypeError, "#{k} not JSON-serializable" unless [String, Integer, Float, Hash, Array, NilClass, TrueClass, FalseClass].any? { |c| v.is_a?(c) } }

Try / catch

begin
  stream = Baml.stream_function(fn, args, ctx, ENV.to_h)
rescue SyntaxError => e
  raise unless e.message.include?('error while parsing stream_function args')
  logger.error("Streaming args invalid for #{fn}: #{e.message}") ; raise
end

Prevention

When it happens

Trigger: Baml.stream_function(name, args, ...) with args containing non-JSON-serializable Ruby values (symbols, Date/Time, custom objects, IO handles).

Common situations: Streaming variant of arg serialization problems: passing raw Ruby objects for fields the prompt expects as strings; forgetting .to_json-compatible types in nested hashes.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/ad3d3996913e6fde. Report an issue: GitHub.