fix(stt): send convert form fields unencoded (#819) - #823
Closed
kraenhansen wants to merge 2 commits into
Closed
kraenhansen wants to merge 2 commits into
kraenhansen wants to merge 2 commits into
Conversation
The generated raw client started passing keyterms, entity_detection, entity_redaction and webhook_metadata through json.dumps, so a list of keyterms reached the API as a single field holding the whole JSON array and was rejected by the 50 character per-keyterm limit. Omitted fields were sent as the literal string "null" and plain strings arrived quoted. Decode those fields in the hand-maintained speech-to-text wrapper before the request is sent, so lists become repeated form fields, strings are sent verbatim, omitted fields are dropped, and objects stay JSON encoded. Co-authored-by: Kræn Hansen <mail@kraenhansen.dk>
…s encoding Co-authored-by: Kræn Hansen <mail@kraenhansen.dk>
Member
Author
|
Closing and waiting for an upstream fix by the Fern team 🤞 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #819.
Problem
The
SDK regeneration (#817)regeneration changed four multipart fields ofPOST /v1/speech-to-textfrom being passed through as-is to being wrapped injson.dumps(jsonable_encoder(...)):That has three consequences on the wire, all of which are visible when dumping the multipart body of
client.speech_to_text.convert(file=..., model_id="scribe_v2", keyterms=["hello", "world"])onmain:["hello", "world"]as a single keyterm and returnsinvalid_keyword_length— the reported bug.null, becausejsonable_encodermaps theOMITsentinel toNoneandjson.dumps(None)is"null", which is neitherOMITnorNoneand therefore survives the request body filtering.entity_detection="pii"is sent quoted, as"pii".Fix
The generated files can't be edited, so the decoding happens in the hand-maintained
speech_to_text_custom.py. Both speech-to-text clients now point their raw client at a delegating client wrapper whose HTTP client decodes the affected fields just before the request is sent:nullcauses the field to be droppedwebhook_metadata) stays JSON encoded, which is what the API expectsEverything else on the client wrapper and HTTP client is delegated untouched, and only the speech-to-text raw client is affected — the shared client wrapper instance is not mutated, so no other resource sees the interception.
With the fix, the same call sends:
Tests
tests/test_stt_multipart.pycaptures the multipart body through anhttpx.MockTransportand asserts the encoding of each affected field for the sync, async andwith_raw_responsepaths, plus a case pinning that unrelated fields are unchanged. Nine of the ten tests fail onmainand all pass with the fix.Relationship to the upstream generator fix
fern-api/fern#17226 fixes this in the generator, but it does not make this wrapper redundant on its own:
keyterms(Optional[List[str]]) but notentity_detectionandentity_redaction(Union[str, List[str]]) orwebhook_metadata(Union[str, Dict[str, Any]]), which are unions rather than lists and keep thejson.dumpspath, so a plain string stays quoted."null"fix (theis not OMIT else OMITguard) shipped in generator 5.0.4, and.fern/metadata.jsonpinsfernapi/fern-python-sdkto4.64.1— the last 4.x release, published two days before 5.0.4. Picking up either fix requires bumping the generator to 5.x.Decoding only touches string values, so each field stops being rewritten as soon as the generator stops encoding it: after the generator upgrade
keytermsarrives as a list and omitted fields arrive asOMIT, neither of which is a string. The wrapper can be deleted once all four fields are generated correctly.Not covered
dubbing.project.create(keyterms=...),music.compose(tags=...),voices.ivc.create(labels=...),studio.projects.create(genres=...)andaudio_native.create(...)got the samejson.dumpstreatment in the same regeneration. Those resources have no hand-maintained wrapper to hook into, so they are left for the generator upgrade.