Skip to content

fix coercion service not selecting correct rule#2660

Merged
ProgrammerIn-wonderland merged 1 commit intoHeyPuter:mainfrom
P3il4:fix-coercion
Mar 14, 2026
Merged

fix coercion service not selecting correct rule#2660
ProgrammerIn-wonderland merged 1 commit intoHeyPuter:mainfrom
P3il4:fix-coercion

Conversation

@P3il4
Copy link
Copy Markdown
Contributor

@P3il4 P3il4 commented Mar 13, 2026

currently coerce method always returns a value even when no coercion is found. this causes a "string:url:data" type result to run through a "string:url:web -> stream" coercion instead of the intended "string:url:data -> stream", resulting in issues such as undefined content_type and passing a base64 data url into the web url processing logic.
changed the method to return undefined in case no valid coercion was found and to allow it to try others.

side note: TypeSpec.equals() only checks the base value ($) ignoring content_type. this means a content_type: video result can get routed through a content_type: image rule. this doesn't break anything right now as they're almost the same, but something to keep in mind.

class TypeSpec extends BasicBase {
static adapt (raw) {
if ( raw instanceof TypeSpec ) return raw;
return new TypeSpec(raw);
}
constructor (raw) {
super();
this.raw = raw;
}
equals (other) {
return this.raw.$ === other.raw.$;
}

@ProgrammerIn-wonderland
Copy link
Copy Markdown
Collaborator

@KernelDeimos does this look correct?

@KernelDeimos
Copy link
Copy Markdown
Contributor

This does look correct. How do we test this? (i.e. through which driver/feature was the problem observed and what steps were taken to produce it?)

@P3il4
Copy link
Copy Markdown
Contributor Author

P3il4 commented Mar 14, 2026

This does look correct. How do we test this? (i.e. through which driver/feature was the problem observed and what steps were taken to produce it?)

original issue came out of the gemini image generation driver causing this error when requesting a 2K quality image with flash-image-3.1:

{"success":false,"error":{"key":"url","expected":"web URL","got":"Maximum call stack size exceeded","code":"field_invalid","$":"heyputer:api/APIError","message":"Field `url` is invalid. Expected web URL. Got Maximum call stack size exceeded.","status":400}}

i traced the issue to secureAxiosRequest in securehttp.js. i wasn't able to reproduce the crash locally, but later realized that it shouldn't have went this way at all, as the gemini service was returning a string:url:data, while secureAxiosRequest was only supposed to be for string:url:web types.

const coercions = this.coercions_.filter(coercion => {
const produces = TypeSpec.adapt(coercion.produces);
return target.equals(produces);
});
for ( const coercion of coercions ) {
const available = await this.coerce(coercion.consumes, typed_value);
if ( ! available ) continue;
const coerced = await coercion.coerce(available);
typed_value.calculated_coercions_[target_hash] = coerced;
return coerced;
}
return typed_value;

putting a few console logs around this spot i noticed that it was using the

produces: {
    $: 'stream',
    content_type: 'image',
},
consumes: {
    $: 'string:url:web',
    content_type: 'image',
},

coercion, resulting in odd behavior on smaller images too, such as content-type being "undefined"
applying my fix it now tries all available coercions until it finds the one it needs, no longer going into securehttp at all and responding with the correct content-type.

before the fix:
(logging desired_type and result in driverservice and the selected coercions in coercionservice)
image
after the fix:
image

you can test this on live puter.com right now:

puter.ai.txt2img('generate an image', options= {
    ratio: {"w": 1, "h": 1},
    quality: "1K",
    model: "google:google/gemini-3.1-flash-image-preview",
})

going into network tab, the /call will have content-type: undefined.

puter.ai.txt2img('generate an image', options= {
    ratio: {"w": 1, "h": 1},
    quality: "2K",
    model: "google:google/gemini-3.1-flash-image-preview",
})

and this will result in the error mentioned above

@ProgrammerIn-wonderland ProgrammerIn-wonderland merged commit 89904ca into HeyPuter:main Mar 14, 2026
3 of 4 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.

3 participants