TypeError: AutomaticSpeechRecognitionPipeline._sanitize_parameters() got an unexpected keyword argument 'max_new_tokens'

Following the course locally. In the latter part of lesson 6 we use

asr = pipeline(task="automatic-speech-recognition",
               model="distil-whisper/distil-small.en")

and later define this function

def transcribe_long_form(filepath):
    if filepath is None:
        gr.Warning("No audio found, please retry.")
        return ""
    output = asr(
      filepath,
      max_new_tokens=256,
      chunk_length_s=30,
      batch_size=8,
    )
    return output["text"]

But running locally it throws this error:

  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/transformers/pipelines/base.py", line 1417, in __call__
    preprocess_params, forward_params, postprocess_params = self._sanitize_parameters(**kwargs)
                                                            ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: AutomaticSpeechRecognitionPipeline._sanitize_parameters() got an unexpected keyword argument 'max_new_tokens'

Where might this error originate?

1 Like

hi @Peter_S2

is this the complete error?

Remember the self. function here it is using might be based on metadata files, so check while recalling the sanitization function you are following the same code implementation as the course provided.

you can check this with requirements.txt file provided in file==>Open section which might provide you the module version as module version discrepancies cam also cause such error

Greetings Deepti, thanks for the quick response.
Would you please direct me to this ā€œfile=>Openā€ section.
The Jupyter notebook has a File menu with an Open… choice. However I don’t see anything called requirements in any of the folders shown there. Also checked the .cache directory locally, not there.
Many thanks.

Also, complete error below:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/queueing.py", line 626, in process_events
    response = await route_utils.call_process_api(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/route_utils.py", line 322, in call_process_api
    output = await app.get_blocks().process_api(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<11 lines>...
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/blocks.py", line 2229, in process_api
    result = await self.call_function(
             ^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<8 lines>...
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/blocks.py", line 1740, in call_function
    prediction = await anyio.to_thread.run_sync(  # type: ignore
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        fn, *processed_input, limiter=self.limiter
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 2470, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 967, in run
    result = context.run(func, *args)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/utils.py", line 940, in wrapper
    response = f(*args, **kwargs)
  File "/Users/pspry/Documents/AI/lesson6.py", line 28, in transcribe_long_form
    output = asr(
      filepath,
    ...<2 lines>...
      batch_size=8,
    )
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/transformers/pipelines/automatic_speech_recognition.py", line 271, in __call__
    return super().__call__(inputs, **kwargs)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/transformers/pipelines/base.py", line 1417, in __call__
    preprocess_params, forward_params, postprocess_params = self._sanitize_parameters(**kwargs)
                                                            ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: AutomaticSpeechRecognitionPipeline._sanitize_parameters() got an unexpected keyword argument 'max_new_tokens'
1 Like

Peter can you directly provide me to link page to lab you are working ( note I want link to the course provided lab)

this looks like module version issue but I need to see how the codes were given and how you wrote locally

Hi Deepti, the lesson is: https://s172-29-106-101p8888.lab-aws-production.deeplearning.ai/notebooks/L07/L7_Text_to_Speech.ipynb
If I download the Jupyter notebook and run it in a local install of Jupyter I do not get this error. However, running the code as a python script (included below) I continue to get the ā€˜max_new_token’ error. I have double checked and confirmed that I have satisfied all the library requirements listed in the lab.
Running on MacBook Pro 2024, Sequoia 15.2 in Python 3.13

#!/usr/bin/env python
# coding: utf-8

# # Lesson 6: Automatic Speech Recognition

# - In the classroom, the libraries are already installed for you.
# - If you would like to run this code on your own machine, you can install the following:
# ``` 
#     !pip install transformers
#     !pip install -U datasets
#     !pip install soundfile
#     !pip install librosa
#     !pip install gradio
# ```
# 
# The `librosa` library may need to have [ffmpeg](https://www.ffmpeg.org/download.html) installed. 
# - This page on [librosa](https://pypi.org/project/librosa/) provides installation instructions for ffmpeg.

# - Here is some code that suppresses warning messages.

from transformers.utils import logging
logging.set_verbosity_error()


# Build the pipeline 
from transformers import pipeline


# Build pipeline
asr = pipeline(task="automatic-speech-recognition",
               model="distil-whisper/distil-small.en")


# Info about [distil-whisper/distil-small.en](https://huggingface.co/distil-whisper)


# ### Build a shareable app with Gradio
# 
# ### Troubleshooting Tip
# - Note, in the classroom, you may see the code for creating the Gradio app run indefinitely.
#   - This is specific to this classroom environment when it's serving many learners at once, and you won't wouldn't experience this issue if you run this code on your own machine.
# - To fix this, please restart the kernel (Menu Kernel->Restart Kernel) and re-run the code in the lab from the beginning of the lesson.

import os
import gradio as gr

demo = gr.Blocks()

def transcribe_long_form(filepath):
    if filepath is None:
        gr.Warning("No audio found, please retry.")
        return ""
    output = asr(
      filepath,
      max_new_tokens=256,
      chunk_length_s=30,
      batch_size=8,
    )
    return output["text"]

mic_transcribe = gr.Interface(
    fn=transcribe_long_form,
    inputs=gr.Audio(sources="microphone",
                    type="filepath"),
    outputs=gr.Textbox(label="Transcription",
                       lines=3),
    allow_flagging="never")

file_transcribe = gr.Interface(
    fn=transcribe_long_form,
    inputs=gr.Audio(sources="upload",
                    type="filepath"),
    outputs=gr.Textbox(label="Transcription",
                       lines=3),
    allow_flagging="never",
)

with demo:
    gr.TabbedInterface(
        [mic_transcribe,
         file_transcribe],
        ["Transcribe Microphone",
         "Transcribe Audio File"],
    )
demo.launch(share=False, 
            server_port=8000) #int(os.environ['PORT1']))


# demo.close()


# ## Note: Please stop the demo before continuing with the rest of the lab.
# - The app will continue running unless you run
#   ```Python
#   demo.close()
#   ```
# - If you run another gradio app (later in this lesson) without first closing this appp, you'll see an error message:
#   ```Python
#   OSError: Cannot find empty port in range
#   ```


1 Like

Locally, as a Jupyter notebook, I get no errors. Locally in Python (or IDLE) I get this error:

* Running on local URL:  http://127.0.0.1:8000
* To create a public link, set `share=True` in `launch()`.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/queueing.py", line 626, in process_events
    response = await route_utils.call_process_api(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/route_utils.py", line 322, in call_process_api
    output = await app.get_blocks().process_api(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<11 lines>...
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/blocks.py", line 2229, in process_api
    result = await self.call_function(
             ^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<8 lines>...
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/blocks.py", line 1740, in call_function
    prediction = await anyio.to_thread.run_sync(  # type: ignore
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        fn, *processed_input, limiter=self.limiter
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 2470, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 967, in run
    result = context.run(func, *args)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/gradio/utils.py", line 940, in wrapper
    response = f(*args, **kwargs)
  File "/Users/pspry/Downloads/L6_Automatic_Speech_Recognition.py", line 53, in transcribe_long_form
    output = asr(
      filepath,
    ...<2 lines>...
      batch_size=8,
    )
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/transformers/pipelines/automatic_speech_recognition.py", line 271, in __call__
    return super().__call__(inputs, **kwargs)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/transformers/pipelines/base.py", line 1417, in __call__
    preprocess_params, forward_params, postprocess_params = self._sanitize_parameters(**kwargs)
                                                            ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: AutomaticSpeechRecognitionPipeline._sanitize_parameters() got an unexpected keyword argument 'max_new_tokens'

2 Likes

understood your issue, finding solution please wait.

until then please check the modules you installed in system matches with the course provided version ESPECIALLY transformer because your pipeline is invoking this error.

Also confirm if made sure the utils.py file and/or metadata changes were considered while writing this python script as this could have also caused issue because in course provided they must have defined a function to include the max token to the pipeline.

@Peter_S2

please add this statement

from transformers import AutoModelForCausalLM, AutoTokenizer,AutoModelForSeq2SeqLM and check if the issue gets resolved.

add it after you wrote statement

from transformer import pipeline, or if you want you can merge

let me know if you still get the issue

Hi Deepti, no change after adding the additional import. I.E. same error appears.
Checking dependencies in local python vs the Jupyter notebook I see find:
Local Python: transformers 4.53.1
Lesson notebook: transformers 4.37.2
And the file in site-packages/transformers/generation/utils.py uses the term max_new_tokens 20 times. Unfortunately, downgrading transformers to 4.37.1 generates a host of incompatibility dependency conflicts.
I’m not sure where I can check for metadata changes you mention.

1 Like

utils.py file and other files (model) is called metadata.

I was looking at the lab codes, can I ask you, did you download the distill whisper small.en also? locally.

max new token being 20 is a default max value, here is a git hub link explaining the same

you could download the required transformer version with pip install statement in the notebook you are working upon, here is a link explaining the same Link

Try this and let me know if this helps.

I also got to know you need to download distill whisper small en checkpoint locally in your system for the speech recognition model to work. so make sure you do that too.

If you notice in the lab you shared, in the file==>Open section, there is a folder model (so they have downloaded all the required models to run the code successfully) and probably that is what is missing in your local system.

hey Peter I was able to successfully run the codes in Google colab, sharing the codes here, no other files required.

here is updated correction I did

AutomaticSpeechRecognition.ipynb (126.8 KB)

Hi Deepti, I downgraded transformers to 4.37.2 and still had the max_new_tokens error. I finally removed that parameter from call and it is working fine.

output = asr(
      filepath,
      max_new_tokens=256,
      chunk_length_s=30,
      batch_size=8,
    )
    return output["text"]

to be

output = asr(
      filepath,
      chunk_length_s=30,
      batch_size=8,
    )
    return output["text"]

i shared the notebook with bit of corrections here and there where it worked perfectly fine for me with the max new token parameter