Closed damadorPL closed 2 weeks ago
To resolve the "local variable 'api_key' referenced before assignment" error in the "Text Preprocessing and Cleaning" step when using Jina with Dify version 0.9.1 (self-hosted via Docker), ensure that the api_key
variable is properly assigned before it is used. In the provided code, the api_key
parameter in the translate_text
function has a default value set to DIFY_API_KEY
.
Here is the relevant part of the code:
DIFY_API_KEY = "example-key"
def translate_text(api_key=DIFY_API_KEY, user_id="mark-doc", inputs={'input_text': 'Hello, how are you?', 'target_language': 'es'}):
url = 'https://api.dify.ai/v1/workflows/run'
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
data = {
'inputs': inputs,
'response_mode': 'blocking',
'user': user_id
}
response = requests.post(url, headers=headers, json=data)
try:
response.raise_for_status()
json_response = response.json()
return json_response['data']['outputs']
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except requests.exceptions.RequestException as req_err:
print(f'Request error occurred: {req_err}')
except json.decoder.JSONDecodeError:
print('Failed to decode JSON response')
return None
The api_key
is correctly assigned a default value of DIFY_API_KEY
. If you are still encountering the error, ensure that DIFY_API_KEY
is defined and not overwritten or modified elsewhere in your code.
If you need to pass a different api_key
dynamically, make sure to pass it explicitly when calling the translate_text
function:
translated_content = translate_text(api_key="your_actual_api_key", inputs={'input_text': content, 'target_language': target_language})
This should resolve the "local variable 'api_key' referenced before assignment" error [1].
To continue talking to Dosu, mention @dosu.
Self Checks
Dify version
0.9.1
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
followup to https://github.com/langgenius/dify/issues/8934
now when it comes to "Text Preprocessing and Cleaning"
there pop up local variable 'api_key' referenced before assignment using
✔️ Expected Behavior
No response
❌ Actual Behavior
/console/api/datasets/indexing-estimate 500 (Internal Server Error)