@@ -401,70 +401,59 @@ def ask_question(self, question, conversation_id: Optional[str] = None):
401401 Returns:
402402 GenieResponse with result, query, description, and conversation_id
403403 """
404- try :
405- args = {"query" : question }
406- if conversation_id :
407- args ["conversation_id" ] = conversation_id
408-
409- mcp_result = self ._mcp_client .call_tool (self ._tool_name , args )
410-
411- if not mcp_result .content or len (mcp_result .content ) == 0 :
412- return GenieResponse (
413- result = "No content returned from Genie" ,
414- conversation_id = conversation_id ,
415- )
404+
405+ args = {"query" : question }
406+ if conversation_id :
407+ args ["conversation_id" ] = conversation_id
416408
417- # Genie backend always returns exactly 1 content block with JSON
418- content_block = mcp_result .content [0 ]
419- content_text = content_block .text if hasattr (content_block , "text" ) else "{}"
409+ mcp_result = self ._mcp_client .call_tool (self ._tool_name , args )
420410
421- try :
422- genie_response = json .loads (content_text )
423- except json .JSONDecodeError :
424- return GenieResponse (
425- result = f"Failed to parse response: { content_text } " ,
426- conversation_id = conversation_id ,
427- )
428-
429- content = genie_response .get ("content" , "" )
430- conv_id = genie_response .get ("conversationId" , conversation_id )
431- status = genie_response .get ("status" , "" )
432-
433- try :
434- content_data = json .loads (content )
435- query_str = content_data .get ("query" , "" )
436- description = content_data .get ("description" , "" )
437- statement_response = content_data .get ("statement_response" )
438-
439- if (
440- statement_response
441- and statement_response .get ("status" , {}).get ("state" ) == "SUCCEEDED"
442- ):
443- result = _parse_query_result (
444- statement_response , self .truncate_results , self .return_pandas
445- )
446- else :
447- result = content
411+ if not mcp_result .content or len (mcp_result .content ) == 0 :
412+ return GenieResponse (
413+ result = "No content returned from Genie" ,
414+ conversation_id = conversation_id ,
415+ )
448416
449- except (json .JSONDecodeError , KeyError , TypeError , AttributeError ):
450- result = content
417+ # Genie backend always returns exactly 1 content block with JSON
418+ content_block = mcp_result .content [0 ]
419+ content_text = content_block .text if hasattr (content_block , "text" ) else "{}"
451420
421+ try :
422+ genie_response = json .loads (content_text )
423+ except json .JSONDecodeError :
452424 return GenieResponse (
453- result = result ,
454- query = query_str ,
455- description = description ,
456- conversation_id = conv_id ,
425+ result = f"Failed to parse response: { content_text } " ,
426+ conversation_id = conversation_id ,
457427 )
458428
459- except Exception as e :
460- # Fall back to deprecated REST methods
461- logging .warning (f"MCP call failed, falling back to REST API: { e } " )
429+ content = genie_response .get ("content" , "" )
430+ conv_id = genie_response .get ("conversationId" , conversation_id )
431+ status = genie_response .get ("status" , "" )
432+ query_str = ""
433+ description = ""
462434
463- if not conversation_id :
464- resp = self .start_conversation (question )
435+ try :
436+ content_data = json .loads (content )
437+ query_str = content_data .get ("query" , "" )
438+ description = content_data .get ("description" , "" )
439+ statement_response = content_data .get ("statement_response" )
440+
441+ if (
442+ statement_response
443+ and statement_response .get ("status" , {}).get ("state" ) == "SUCCEEDED"
444+ ):
445+ result = _parse_query_result (
446+ statement_response , self .truncate_results , self .return_pandas
447+ )
465448 else :
466- resp = self .create_message (conversation_id , question )
467- genie_response = self .poll_for_result (resp ["conversation_id" ], resp ["message_id" ])
468- if not genie_response .conversation_id :
469- genie_response .conversation_id = resp ["conversation_id" ]
470- return genie_response
449+ result = content
450+
451+ except (json .JSONDecodeError , KeyError , TypeError , AttributeError ):
452+ result = content
453+
454+ return GenieResponse (
455+ result = result ,
456+ query = query_str ,
457+ description = description ,
458+ conversation_id = conv_id ,
459+ )
0 commit comments